// File loading here, 1 byte at a time.
if( LastByte == EOF )
break;
simple.
EOF means End Of File BTW.
Eric
Arrays
ok that´s neat, but actually my code reads the whole sentence of a txt file,
so how could i get to the last byte?
so how could i get to the last byte?
if the value read from the file equals EOF then it is ended. This value is defined in stdio.h which must be included.
Jacob Marner, M.Sc.Console Programmer, Deadline Games
hm ok sorry for keep on bothering you
i now understand the base of vectors :D thanks for that m8s!!
so now back to the txt file loading
this doesnt work for a reason.. after the first line it already says it is the end of thefile..
#include "stdafx.h"
#include "stdio.h"
void readstr(FILE *f,char *string)
{
do
{
fgets(string, 255, f);
} while ((string[0] == ''/'') || (string[0] == ''\n''));
return;
}
int main(int argc, char* argv[])
{
FILE *filein;
char oneline[255];
bool fileend;
fileend=false;
if((filein=fopen("hello.txt", "rt"))==NULL)
printf("File does not exist");
while(fileend==false){
if(oneline[0]=EOF){
fileend=true;
printf("EINDE FILE
}
readstr(filein,oneline);
printf(oneline);
}
fclose(filein);
return 0;
}
this is the txt file:
hello
bla
blalba
thanks!
Jan
(btw yes a part of it is from the nehe tutorial.. kept on the way they open and read files )
i now understand the base of vectors :D thanks for that m8s!!
so now back to the txt file loading
this doesnt work for a reason.. after the first line it already says it is the end of thefile..
#include "stdafx.h"
#include "stdio.h"
void readstr(FILE *f,char *string)
{
do
{
fgets(string, 255, f);
} while ((string[0] == ''/'') || (string[0] == ''\n''));
return;
}
int main(int argc, char* argv[])
{
FILE *filein;
char oneline[255];
bool fileend;
fileend=false;
if((filein=fopen("hello.txt", "rt"))==NULL)
printf("File does not exist");
while(fileend==false){
if(oneline[0]=EOF){
fileend=true;
printf("EINDE FILE
}
readstr(filein,oneline);
printf(oneline);
}
fclose(filein);
return 0;
}
this is the txt file:
hello
bla
blalba
thanks!
Jan
(btw yes a part of it is from the nehe tutorial.. kept on the way they open and read files )
I think you should start your debugger and learn to find these errors yourself instead of posting here immediately. YOu will never get better if we tell you the answer everytime.
You wrote:
if(oneline[0]=EOF){
I think you mean:
if(oneline[0]==EOF){
Oh, and remember to check for EOF in readstr() also.
[edited by - felonius on June 6, 2002 8:54:23 AM]
You wrote:
if(oneline[0]=EOF){
I think you mean:
if(oneline[0]==EOF){
Oh, and remember to check for EOF in readstr() also.
[edited by - felonius on June 6, 2002 8:54:23 AM]
Jacob Marner, M.Sc.Console Programmer, Deadline Games
well,
actually i am busy on finding the answer for about 4 days now.. i am new on this forum, and i thought if i cannot find it myself the nask..
besides..
the code you gave doesn't work
it keeps repeating the last sentence of the file until i break it by shutting the program down...
[edited by - Fluid FX on June 6, 2002 9:47:56 AM]
actually i am busy on finding the answer for about 4 days now.. i am new on this forum, and i thought if i cannot find it myself the nask..
besides..
the code you gave doesn't work
it keeps repeating the last sentence of the file until i break it by shutting the program down...
[edited by - Fluid FX on June 6, 2002 9:47:56 AM]
Hey Fluid,
Here''s a simple example, note syntax has not been checked with a compiler so I may be wrong here in a few places.
#include <cstdio>
.
.
.
char Buffer[1024];
char* FileName = "testfile.dat";
FILE *FilePtr = NULL;
FilePtr = fopen(FileName, "r+w");
if ( NULL == FilePtr )
{
printf("Error Opening File %s", FileName);
exit(1);
}
while( !feof(FilePtr) )
{
fgets(Buffer, sizeof(Buffer), FilePtr);
}
fclose(FilePtr);
Here''s a simple example, note syntax has not been checked with a compiler so I may be wrong here in a few places.
#include <cstdio>
.
.
.
char Buffer[1024];
char* FileName = "testfile.dat";
FILE *FilePtr = NULL;
FilePtr = fopen(FileName, "r+w");
if ( NULL == FilePtr )
{
printf("Error Opening File %s", FileName);
exit(1);
}
while( !feof(FilePtr) )
{
fgets(Buffer, sizeof(Buffer), FilePtr);
}
fclose(FilePtr);
quote: Original post by Fluid FX
actually i am busy on finding the answer for about 4 days now..
I apologize. You just posted that question very quickly after the other one so it just seemed that you just had run into the problem and didn''t bother trying to figure it out for yourself.
Jacob Marner, M.Sc.Console Programmer, Deadline Games
no offence m8.. :-) i mean i could have posted another topic..
thanks for the help, i will check if this works!!
thank you all
sorry to bother you
Cheers
Jan
thanks for the help, i will check if this works!!
thank you all
sorry to bother you
Cheers
Jan
articles & resources section of this site: search "File i/o"
results:
click here
start there. good tuts. everything you ever wanted to know about file i/o in c++
learn to use the Articles & resources section. it is the best source of information for most problems.
-me
[edited by - Palidine on June 6, 2002 4:45:03 PM]
results:
click here
start there. good tuts. everything you ever wanted to know about file i/o in c++
learn to use the Articles & resources section. it is the best source of information for most problems.
-me
[edited by - Palidine on June 6, 2002 4:45:03 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement