Read This
-
Newbie Question. Ok, I need to write and then latter read multiple lines to a file. I figured out how to write one line to a file, and I think I am close for multiple lines, but I am just doin something wrong.
char test1; char test2; test1 = 1; test2 = 2; FILE *fp=fopen("Test.startup","w"); if(fp){ fprintf(fp,"%d",test1 test2); fclose(fp); }
Well, obviously that isn't working. Latter it is going to be for like 5 vars. So I am going to need to know how to write five vars, and read five vars to and from a file. I think for reading it will be something like this.FILE *fp=fopen("Test.startup","r"); if(fp){ int SomeVar; if(fscanf(fp,"%d",&SomeVar)==1){ // Some Code } fclose(fp); }
Please help. Thanks, Josh -
Newbie Question. Ok, I need to write and then latter read multiple lines to a file. I figured out how to write one line to a file, and I think I am close for multiple lines, but I am just doin something wrong.
char test1; char test2; test1 = 1; test2 = 2; FILE *fp=fopen("Test.startup","w"); if(fp){ fprintf(fp,"%d",test1 test2); fclose(fp); }
Well, obviously that isn't working. Latter it is going to be for like 5 vars. So I am going to need to know how to write five vars, and read five vars to and from a file. I think for reading it will be something like this.FILE *fp=fopen("Test.startup","r"); if(fp){ int SomeVar; if(fscanf(fp,"%d",&SomeVar)==1){ // Some Code } fclose(fp); }
Please help. Thanks, Joshchar test1; char test2; test1 = 1; test2 = 2; FILE *fp=fopen("Test.startup","w"); if(fp){ fprintf(fp,"%d",test1 | test2); fclose(fp); }
Well that didn't work, the file ended up being "3" darn.:(( LOSTTWARE.com -
Newbie Question. Ok, I need to write and then latter read multiple lines to a file. I figured out how to write one line to a file, and I think I am close for multiple lines, but I am just doin something wrong.
char test1; char test2; test1 = 1; test2 = 2; FILE *fp=fopen("Test.startup","w"); if(fp){ fprintf(fp,"%d",test1 test2); fclose(fp); }
Well, obviously that isn't working. Latter it is going to be for like 5 vars. So I am going to need to know how to write five vars, and read five vars to and from a file. I think for reading it will be something like this.FILE *fp=fopen("Test.startup","r"); if(fp){ int SomeVar; if(fscanf(fp,"%d",&SomeVar)==1){ // Some Code } fclose(fp); }
Please help. Thanks, JoshWhy dont you try with fstream instead? i find them to be a lot easier to use. if you need help using fstreams, take a look in here http://www.cpp-home.com/FileIO_tutorial.php its a good tutorial on them. hope this helps
-
Why dont you try with fstream instead? i find them to be a lot easier to use. if you need help using fstreams, take a look in here http://www.cpp-home.com/FileIO_tutorial.php its a good tutorial on them. hope this helps
Ok, I am getting closer, still getting errors, but so far I have this,
ifstream OpenFile("VARS.startup"); char line[6]; while(!OpenFile.eof()) { OpenFile.getline(line,6); // cout << ch; var_toolbar1 << line; var_toolbar2 << line; var_toolbar3 << line; var_toolbar4 << line; var_LCLWindow << line; var_OUTWindow << line; } OpenFile.close();
I declared the vars as intergers in the header file, and each line contains a 0 or a 1. The file has 6 lines for now. I need to get each line into it's respectful Var. LOSTTWARE.com -
Newbie Question. Ok, I need to write and then latter read multiple lines to a file. I figured out how to write one line to a file, and I think I am close for multiple lines, but I am just doin something wrong.
char test1; char test2; test1 = 1; test2 = 2; FILE *fp=fopen("Test.startup","w"); if(fp){ fprintf(fp,"%d",test1 test2); fclose(fp); }
Well, obviously that isn't working. Latter it is going to be for like 5 vars. So I am going to need to know how to write five vars, and read five vars to and from a file. I think for reading it will be something like this.FILE *fp=fopen("Test.startup","r"); if(fp){ int SomeVar; if(fscanf(fp,"%d",&SomeVar)==1){ // Some Code } fclose(fp); }
Please help. Thanks, Josh- fprintf(fp,"%d",test1 test2); How did this compile? If the line was "fprintf(fp,"%d",test1, test2);" then it would make since. OH by the way: fopen("Test.startup","w") and fopen("Test.startup","r") assumes that you are loading binary data not text data. Use fopen("Test.startup","wt") and fopen("Test.startup","rt") when working with text file. If you want to store characters '1' and '2' to a file:
char test1 = '1';
char test2 = '2';
FILE *fp = fopen("Test.startup","wt");
if(fp)
{
fprintf(fp,"%c%c",test1, test2);
fclose(fp);
}If you want to load characters '1' and '2' from a file:
char test1;
char test2;FILE *fp=fopen("Test.startup","rt");
if(fp)
{
if( fscanf(fp,"%c%c", &test1, &test2) )
{
// Some Code
}
fclose(fp);
}From what I have seen, all you need to do is study the documentation for file I/O. I know peaple ar going to throw iostream a you as a suposably better solution. But 90% of the time it is just a wrapper for for what you are doing. Writing chacaters to a file using fprintf() is ok, but reading them with fsprintf() has (almost always) been a joke. Before you use iostream you must understand reading a file at the system level (a.k.a. C level). INTP
-
- fprintf(fp,"%d",test1 test2); How did this compile? If the line was "fprintf(fp,"%d",test1, test2);" then it would make since. OH by the way: fopen("Test.startup","w") and fopen("Test.startup","r") assumes that you are loading binary data not text data. Use fopen("Test.startup","wt") and fopen("Test.startup","rt") when working with text file. If you want to store characters '1' and '2' to a file:
char test1 = '1';
char test2 = '2';
FILE *fp = fopen("Test.startup","wt");
if(fp)
{
fprintf(fp,"%c%c",test1, test2);
fclose(fp);
}If you want to load characters '1' and '2' from a file:
char test1;
char test2;FILE *fp=fopen("Test.startup","rt");
if(fp)
{
if( fscanf(fp,"%c%c", &test1, &test2) )
{
// Some Code
}
fclose(fp);
}From what I have seen, all you need to do is study the documentation for file I/O. I know peaple ar going to throw iostream a you as a suposably better solution. But 90% of the time it is just a wrapper for for what you are doing. Writing chacaters to a file using fprintf() is ok, but reading them with fsprintf() has (almost always) been a joke. Before you use iostream you must understand reading a file at the system level (a.k.a. C level). INTP
John R. Shaw wrote: Writing chacaters to a file using fprintf() is ok, but reading them with fsprintf() has (almost always) been a joke. Someone has actually suggested that reading from a file can be done via
fsprintf()
?
Five birds are sitting on a fence. Three of them decide to fly off. How many are left?
-
John R. Shaw wrote: Writing chacaters to a file using fprintf() is ok, but reading them with fsprintf() has (almost always) been a joke. Someone has actually suggested that reading from a file can be done via
fsprintf()
?
Five birds are sitting on a fence. Three of them decide to fly off. How many are left?
Doh!:-O I was not thinking clearly. I just made some modifications to the sample code that was given. Besides it should not have been fsprintf (does this exist), it should have been fscanf. INTP