C++ :: How To Save Struct To File And Then Read It Back From The File
Aug 5, 2013
How to save the struct to file and then read it back from the file ?
Code:
#include <conio.h>
#include <stdio.h>
struct student {
int person;
int egn;
float AvergeGrade;
[Code] ....
Have average grade: %f", person[i].FirstName, person[i].LastName, person[i].egn, person[i].AvergeGrade);
}
//Save to file
getch ();
}
View 2 Replies
ADVERTISEMENT
Mar 15, 2013
The Objective Of This Program Is To Create A File To Write Text And Read Back The File Content. To Do That I Have Made Two Function writeFile() To Write And readFile() To Read.The readFile() function works just fine but writeFile() doesn't.
How writeFile() function Works? when writeFile() function Execute It Takes Characters User Type And When Hit Enter(ASC|| 10) It Ask "More?(Y/N)" That Means What User Want? Want To Go Next Line Or End Input?
If "Y" Than Inputs Are Taken From Next Line Else Input Ends.
But The Problem Is When Program Encounters ch==10 It Shows "More?(Y/N)" And Takes Input In cmd variable.If cmd=='Y' I Mean More From Next Line Than It Should Execute Scanf Again To Take ch I Mean User Input.But Its Not!!! Its Always Showing "More?(Y/N)" Again And Again Like A Loop.
Code:
#include <stdio.h>
void writeFile(void);
void readFile(void);
int main(){
[Code].....
View 5 Replies
View Related
Nov 5, 2013
I am in an "intro" C++ course and am having trouble with a section of my current project.
We are required to open a text file, read the data from that file, and print it back.
However, we are supposed to let the user input the name of the file that is to be opened. This is the code I have so far. It is not opening anything.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int NUM_QUESTIONS = 30;
typedef char CharArray[NUM_QUESTIONS];
string fileName;
[Code]...
View 2 Replies
View Related
Aug 21, 2014
I would like to write a complete structure array to a file and read it back, recovering all the data. I have tried the following:
Code:
#include <stdio.h>
#include <string.h>
#define NUM 256
const char *fname="binary.bin";
typedef struct foo_s {
int intA;
int intB;
char string[20];
[Code]...
//---------------------------------------------------- but the mac field is reading back some random value repeatedly. Why is that? And how do I fix this?
View 8 Replies
View Related
Jun 18, 2013
I am having a structure called matrix as shown below to represent a matrix your read from a text file.
struct matrix
{
double mat[30][30]; //content of the matrix mat
int row; //number of row in the matrix mat
int col; ////number of col in the matrix mat
};
Declare 3 variables of structure matrix, m1, m2 and m3 in main to be used as describe below.
Task:
1.Write a function void readFile(char *filename, matrix *m) that will read a matrix from a text file and copy to the structure matrix m.
View 5 Replies
View Related
Oct 12, 2014
I have this file want to read it source code [URL] ....
View 10 Replies
View Related
Jan 31, 2014
I am trying to use struct to store variables from a text file and use them in the main program. I have first tried running the program without using struct and declaring the variables within the main program and it runs fine. But after using struct, it gives no compilation error and a segmentation fault as output. Also, if the size of file/variable size is unknown can I declare variables as char string[]??
The code is as below:
Code:
#include<stdio.h>
struct test {
char string1[10000];
[Code].....
View 4 Replies
View Related
Nov 23, 2013
This is a c++ Code. I want to read a address to a pointer. When I try to do so it generates an error. I did is >> obj1[0].contentAdress; in main.
struct node3{
node3(){
nextContent = NULL;
for (int i = 0; i<1020; i++)
content[i] = '';
[code]...
no operator found which takes a right-hand operand of type 'node3 *' (or there is no acceptable conversion)
View 12 Replies
View Related
Nov 5, 2013
I am trying to develop a GUI using MFC, but I am having trouble using CFiledialog to save a file. The problem is, the file is not getting saved to the folder when I use the CFiledialog. Below is the code I am using.
Code:
CString szFilter = "XNRep Files (*.xnrep)|*.xnrep||";
CString s = "xnrep";
CString t = "";
CFileDialog fileDlg(FALSE, s, t, NULL, szFilter);
if(fileDlg.DoModal() == IDOK)
{
std::ofstream file;
[Code]....
After the file dialog opens up, I enter the name of the file and select OK button. But the file does not show up in the directory I am saving to.
View 4 Replies
View Related
Jul 22, 2013
I'm having a little problem with std:fstream - in my program, the user selects the location of a file which I want to remember. So, I have something like this:
Code: std::string fileLocation;
//Code here creates an 'open file' dialog box which lets the user choose which file to open.
//The string 'fileLocation' now contains the path to the chosen file.
std::ofstream prefs("prefs.txt");
if (prefs.is_open())
{
prefs << fileLocation;
prefs.close();
}
This works fine if the file chosen is in the same directory as the program, however, if they try to choose a directory outside of where the program is kept, it saves the text file into that directory instead of the same one as the program. So, it looks like outputting a directory into an ofstream actually changes the location to which the file is saved.
Is there a way to save the file directory to a text file using ofstream and still have the text file save in the same directory as the program?
View 5 Replies
View Related
Oct 10, 2014
How do you create a save file for a game, that is not a separate file? Specifically I am using code::blocks and sfml 2.1 to make a game and it saves to a text file at the moment. My problem is that it is very easy to modify the text file, and it is annoying to have to copy and paste several files if you want to use a copy of the game. I have a feeling that it may be to do with resource files, but I'm not exactly sure how to get these to work or whether you can modify them dynamically.
View 6 Replies
View Related
Sep 28, 2012
I wrote this code behind a button
"
colordialog.showdialog();
File.WriteAllText("dlg.txt",this.BackColor.Name);
"
And I wrote this code in Form Load as well
"
this.BackColor = Color.FromName(File.ReadAllText("dlg.txt"));
"
but I got an error and I dont know what is this.... "Control does not support transparent background colors"
View 2 Replies
View Related
Mar 7, 2013
I am a bit confused about dup2. I am trying to redirect stdout to file and back. It works with a fork(). I'm having trouble making it work without forking. Closing file descriptors has something to do with it...
int main() {
int pid;
int fd;
int defout = dup(1);
fd=open("out.txt", O_RDWR | O_TRUNC | O_CREAT);
dup2(fd, 1); // redirect output to the file
[Code] ....
So what does close have to do with anything?
View 2 Replies
View Related
Aug 13, 2013
I'm still reading Prata's book and there is a section where they explain how to write files using the binary mode.
They define a struct
Code: const int LIM = 20;
struct planet
{
char name[LIM];
double population;
double g;
};
planet pl;
And they say you can use the binary mode to write the whole struct at once (in text mode you need to specify every member).
Code: ofstream fout("planets.dat",
ios_base:: out | ios_base::app | ios_base::binary);
fout.write( (char *) &pl, sizeof pl); I don't understand why they use Code: fout.write( (char *) &pl, sizeof pl) instead of just Code: fout.write( pl, sizeof pl)
View 4 Replies
View Related
Sep 28, 2013
what are the codes that will save the data then it will appear in a file (.txt).
I typed the name "SpongeBob SquarePants"
SpongeBob SquarePants
View 5 Replies
View Related
Jan 12, 2013
I am creating a program using the inheritance. The superclass is person and subclass is employee,manager etc. I will prompt the user to choose which subclass he want to save the record to but i dont know how to write and display the record of different subclass to and from a txt file.
View 4 Replies
View Related
Jan 3, 2014
Code:
struct sth {
char* str;
} pt;
}
[code]...
fprintf does not save string to file. How can I put structure into list?
View 9 Replies
View Related
May 11, 2013
I have a 4*4 array and a 8*8 arays in my c++ program and i would like to save those arrays in one textfile. How do i do that.
View 5 Replies
View Related
Jul 13, 2014
i tried to dave my datagridview to a binary file it worked at first and then when i tried it again it said file is being used by another process.
this is the code that loads the binary file
:
public void loadingparty()
{
string file = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\mygrid.bin";
if (File.Exists(file))
[Code].....
View 2 Replies
View Related
Mar 27, 2014
I'm trying to save a structure to a binary file, then read it out of the binary file into a new structure. I am unsure what is happening but it is not correct.
#include <iostream>
#include <fstream>
using namespace std;
[Code]....
View 2 Replies
View Related
Apr 19, 2014
I tried to save some data from my listbox to the text file, but didn't work... I think I tried many combination for code but still didn't work. what to do?
FileStream fs = new FileStream("tx.txt", FileMode.Create);
StreamWriter sr = new StreamWriter(fs);
sr.Write(listBox1.Text); //there is that problem, but I'm not sure how to write for success
sr.Close();
View 4 Replies
View Related
Oct 10, 2014
When I have loaded a savefile with Serialize, is there some way of telling the name of the currently loaded savefile?
View 2 Replies
View Related
May 19, 2014
I have this code, where I capture window and make copy to bitmap of it. I am working on rotation of bitmap. But my problem is that when I save the image to file the result is just a black screen. It looks like there could be some problem either in GdipCreateBitmapFromHBITMAP() or GdipGetImageGraphicsContext()
How to fix it and to get the rotated image?
Code:
// CODE 81 and 82 de facto no difference
#defineCODE85 // 81
#defineWINDOW_MIN_HEIGHT200
[Code].....
View 14 Replies
View Related
Feb 6, 2014
I am writing a simple file/text parser to read a config file for some code I am working on. It's dead simple and not particularly smart but it should get the job done. The code reads a config file:
Code:
readlength=2500000
start=0
finish=25000000
cutoff=20000
samplingfreq=250000
poles=10
filterpadding=500
}
[code]....
Here is where it gets wierd. You'll notice that there is an unused variable (filepath) in the config struct. This variable is not referenced or used anywhere in the code, ever. Yet if I comment out the declaration of char filepath[1024], the code segfaults partway through the read_config() function.
My best guess is that there is a buffer overflow elsewhere and it just so happens that the memory allocated for filepath happened to be there to catch it up until now, but I can't work out where it might be happening. With the declaration commented out, the read_config() function gets as far as reading the "padding" variable before it crashes. Yet when the declaration is there, then all the variabled are read correctly and everything seems to work.
View 10 Replies
View Related
Mar 8, 2013
Code:
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[])
{
char buffer[256];
FILE * myfile;
myfile = fopen("read.txt","r");
[Code]...
I also got an error in printing wherein the data in read.txt is "Hello" newline "World" and my program prints world twice.
View 3 Replies
View Related
Jun 11, 2014
I have a .txt file which I want to read from and then write a new text file, this time with sorted lines. It is easy to sort one value, but what about sorting entire lines based on one value?
I want to sort the lines based on the FIRST value.
Example text file contents:
values;data
5.01 100
2.9 342
2.69 43534
10.1 43
6.8 45324
Output: Text file containing
2.69 43534
2.9 342
5.01 100
6.8 45324
10.1 43
It's easy to do this if there was only 1 number on each line, but I do I sort all the lines based on the first number in each line?
View 2 Replies
View Related