Hi all.
I have a text file (.txt) with some text lines.
I want read line by line this file. How can i do it??
How can i do it for give me the line in a char variable???
I test with incluing fstream.h file for use file.getline() or other funcitons but dont work me.
Any idea???
I supose that i must use Read() function. I trying it but without results .
Thanks.
Read text files
-
- Posts: 26
- Joined: Sat Jan 03, 2004 3:43 pm
Here is an example in pure C; I hope everything's allright; maybe there are some typing errors
<code>
FILE *in;
char line[1024];
if(!(in=fopen("file.txt","r")))
{
printf("file not found\n");
// need to stop here !!!
}
float blah;
int bloh;
char bluh;
while(!(fgets(line,1024,in)))
{
sscanf(line,"%f %d %c", &blah, &bloh, &bluh);
printf("%f %d %c\n", blah, bloh, bluh);
}
fclose(in);
</code>
<file.txt>
0.1 500 a
-0.12 100 b
0.21 20 c
12.01 -50 d
</file.txt>
regards Tom
<code>
FILE *in;
char line[1024];
if(!(in=fopen("file.txt","r")))
{
printf("file not found\n");
// need to stop here !!!
}
float blah;
int bloh;
char bluh;
while(!(fgets(line,1024,in)))
{
sscanf(line,"%f %d %c", &blah, &bloh, &bluh);
printf("%f %d %c\n", blah, bloh, bluh);
}
fclose(in);
</code>
<file.txt>
0.1 500 a
-0.12 100 b
0.21 20 c
12.01 -50 d
</file.txt>
regards Tom