Stupid C/C++ question
How can i read in a line from a txt file like "bind B jump" and assign each of the 3 words to its own variable (tmpstr1, tmpstr2, tmpstr3).
February 28, 2000 07:38 PM
that''s neither stupid nor a question according to my grammar parser
In C you could use fscanf like this:
char *string1;
char *string2;
char *string3;
FILE *fp;
fp = fopen("dafile.txt","r");
fscanf(fp,"%s",&string1);
fscanf(fp,"%s",&string2);
fscanf(fp,"%s",&string3);
printf("%s %s %s", string1,string2,string3);
Should show:
bind B jump
Or whatever is in the file, I think that is how is it works, sorry Im typing fast!
later,
char *string1;
char *string2;
char *string3;
FILE *fp;
fp = fopen("dafile.txt","r");
fscanf(fp,"%s",&string1);
fscanf(fp,"%s",&string2);
fscanf(fp,"%s",&string3);
printf("%s %s %s", string1,string2,string3);
Should show:
bind B jump
Or whatever is in the file, I think that is how is it works, sorry Im typing fast!
later,
Esap1: after that, how could I make it read the next line and do the same. Your help is greatly appreciated.
um..just loop like he said until fscanf returns an EOF (end of file).
while (fscanf(fp,"%s",&string) != EOF)
{ printf("%s\n", string); }
-oops well that's if it doesn't contain spaces since fscanf stops reading on spaces too, i think you can figure it out heh-
Edited by - shmaLbus on 2/28/00 8:55:34 PM
while (fscanf(fp,"%s",&string) != EOF)
{ printf("%s\n", string); }
-oops well that's if it doesn't contain spaces since fscanf stops reading on spaces too, i think you can figure it out heh-
Edited by - shmaLbus on 2/28/00 8:55:34 PM
-werdup-
using that code as-is will give you a SIGSEV (or segmentation fault, depending on your OS). The problem is you aren't allocating any memory for the char *, you're trying to assign the string to the address of the pointer (if i'm not mistaken). They're pointing to some arbitrary place in memory (basic pointer stuff).
another option would be to use a C++ ifstream object:
the ifstream object treats spaces and '\n' the same, and when it reads EOF, it sets the EOF bit, which you can test for by fin.eof(). In order for that to work, you have to have read EOF, it won't work if EOF is the next character in the stream. To test for that use (fin.peek() == EOF), this examines the next char without removing it from the stream.
of course you can dynamically allocate those char[], but that wasn't your question
oh, almost forgot.. to use cout you need to include iostream.h, and to use ifstream you need to include fstream.h
Edited by - fuzzyai on 2/28/00 9:35:18 PM
another option would be to use a C++ ifstream object:
char string1[12],string2[12],string3[12];
ifstream fin("data.txt",ios::in);
fin >> string1 >> string2 >> string3;
cout << string1 << " " << string2 << " " << string3 << endl;
fin.close();
the ifstream object treats spaces and '\n' the same, and when it reads EOF, it sets the EOF bit, which you can test for by fin.eof(). In order for that to work, you have to have read EOF, it won't work if EOF is the next character in the stream. To test for that use (fin.peek() == EOF), this examines the next char without removing it from the stream.
of course you can dynamically allocate those char[], but that wasn't your question
oh, almost forgot.. to use cout you need to include iostream.h, and to use ifstream you need to include fstream.h
Edited by - fuzzyai on 2/28/00 9:35:18 PM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement