Hi All,
I'm not an expert in C, and what I need to do is the following.

I have a certain large .txt file that has many lines (sometimes the lines are separated with one empty line, sometimes two, sometimes 3,...etc. Some of these lines have "= 9999999999" at their end.

What I need to do is to eliminate such lines (the lines that have "= 9999999999" at their ends).

I'm thinking of reading each line, storing it, checking whether it has that string at its end of not, and then pasting it in a new file if it doesn't (and not doing anything if it does), and so on. That way, at the end I will have a new file that has what I want.

My questions are:

1. What would be the code for reading a whole line?
2. How can I store each line? I mean should I store it in a string or what exactly?
3. How can I search that string or whatever I store that line in, for that certain string on interest at its end ("= 9999999999"). I mean what would the code be in that case?

Thanks a lot and sorry for asking too much!

Aly

Note: Untested and uncompiled code below. Use at your own risk.

FILE* fp = fopen("file.txt","r");
char line[255];
while( fgets(line, sizeof(line), fp) )
{
   int len = strlen(line);
   if( line[len-1] == '\n')
   {
      line[len-1] = 0;
      --len;
   }
   // make test for "= 9999999999" here
   if( (len > 13) && strcmp(&line[len-14],"= 9999999999") == 0)
        ; // do nothing
   else
   {
       // the line is ok, so write it to the output file

   }
}

Thanks a lot, ancient dragon.

My question is: for line 16, I do have to write a code for writing the line in an output file, don't I?

And if so, what would that code be, if you don't mind mentioning that?

Thanks,
Aly

Yes, YOU have to write the code. I did some of your homework for you, so now you can and should finish it up.

Thanks for your help.
I completed the code and I think it works now as I want it to, but I'd appreciate any feedback :-)

Here is the code:

int main (void)
{
 ofstream fout("generator_without_infinities.txt");
      FILE* fp = fopen("generator.txt","r");
   
      char line[1000000];
   
      while( fgets(line, sizeof(line), fp) )
   
      {
   
      int len = strlen(line);
   
      if( line[len-1] == '\n')
   
      {
   
      line[len-1] = 0;
  
      --len;
  
      }
  
      // make test for "= 9999999999" here
  
      if( (len > 12) && strcmp(&line[len-12],"= 9999999999") == 0)
  
      ; // do nothing
  
      else
  
      {
  
      // the line is ok, so write it to the output file
  
      
	   fout<<line;
	fout<<"\n";
      }
  
	  }




	


	return 0;
}

Aly

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.