After fgets get line, how can I search detail in line?

Started by
3 comments, last by fantasyme 13 years, 12 months ago
For example: I have a text file. I want to count the space in line 2 ======================== a a a aa a a a b b bb b b b (line 2) c c c cc c c ========================

char s[256];
FILE* fp;
fp = fopen("test.txt", "r");
while(fgets(s,sizeof(s), fp)
{
 if(s[0] == 'b')
 { 
   (fgets(s,sizeof(s), fp);
   printf("%s", s); // I will get b b bb b b b
  //I want to count space here, I tried to use s and loop for but it doesn't work

 }
}
Advertisement
Quote:Original post by fantasyme
  //I want to count space here, I tried to use s and loop for but it doesn't work
Maybe show what you tried and we can suggest modifications?

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

strtok is useful for counting or splitting a string at a certain character.
sprintf is useful to parse a string into multiple variable types.

But as other will probably say, this is old C and you should use the C++ way with std, will be easier.
I'm trying to compile your code right now, it would have been faster if you put it inside a main w/ all of the headers, just so everyone doesn't have to do it for you...
Your "while(fgets(s,sizeof(s), fp)" is missing a ")". Your "(fgets(s,sizeof(s), fp);" has an extra "(". How'd you get this code to compile?
sorry, it is not the full code.

Actually,I want to take the vector's detail in .x file.
I can get the detail in "char" using fgets() to get the detail by line.

I just want to know some idea to do it.

for example:
-1222;1223332;33403032; // all type char

I want to get the number in type of int.
I still stuck on how to get the detail in line.

I used for loop like...
for(int i = 0; s != ';';i++)
columnNum++;
but s !=';' is not work.
the program didn't find and compare what I want ';'



This topic is closed to new replies.

Advertisement