4 Replies - 269 Views - Last Post: 16 January 2019 - 12:51 AM Rate Topic: -----

#1 aemaeth   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 10-February 17

Using fgets() to read 2 lines from .cfg file

Posted 13 January 2019 - 10:41 PM

Hi guys, I'm trying to use fgets() to read only 2 lines from a .cfg file that has 2 parameters, precision and columns.

I've tested it with just writing one parameter in the .cfg file like "3" and everything worked fine. With "3" on line 1 and a "6" on line 2 however, it seems to be overwriting itself. That is, I did some googling and saw some people saying that same BUFFER is being used and overwriting itself, so only my last parameter columns = 6 (on line 2) was updated, but not my line 1 parameter of "precision".
 
#define  LINELEN  64

int main(int argc, char *argv[])
{
   int cols = 3; // default value for columns is 3.
   int prec = 13; // default value for precision is 13 decimal places.

   char oneLine[LINELEN];
   
   // Override the default value with a value from the configuration file.
   // open the configuration file
   FILE * fp;
   if ( (fp = fopen("filter.cfg", "r")) != NULL )
   {
      // read the first line of the config file
      // where oneLine is BUFFER
      // where LINELEN is sizeof BUFFER
      // where fp is stream
      while( fgets(oneLine, LINELEN, fp) != NULL)
      {
         // get an operand from the config file
         prec = atoi(oneLine); // line 1 of .cfg file i.e. 3, its just a parameter
         cols = atoi(oneLine); // line 2 of .cfg file i.e. 6, its just a parameter
      
      }

 return 0;
}



Is there not an easy to way to simply get it store the first line of the .cfg file into my prec variable AND then simply get it to store the 2nd line of the .cfg file to my cols variable???

This post has been edited by aemaeth: 13 January 2019 - 10:44 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Using fgets() to read 2 lines from .cfg file

#2 Salem_c   User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 2327
  • View blog
  • Posts: 4,437
  • Joined: 30-May 10

Re: Using fgets() to read 2 lines from .cfg file

Posted 14 January 2019 - 12:19 AM

22	         prec = atoi(oneLine); // line 1 of .cfg file i.e. 3, its just a parameter
23	         cols = atoi(oneLine); // line 2 of .cfg file i.e. 6, its just a parameter


Well you are stuffing the same result in both variables every time around the loop.
There is NO distinction in this loop saying "this is line 1" or "this is line 2".

      if( fgets(oneLine, LINELEN, fp) != NULL)
      {
         // get an operand from the config file
         prec = atoi(oneLine); // line 1 of .cfg file i.e. 3, its just a parameter
      }
      if( fgets(oneLine, LINELEN, fp) != NULL)
      {
         cols = atoi(oneLine); // line 2 of .cfg file i.e. 6, its just a parameter
      
      }


Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • quantum multiprover
  • member icon

Reputation: 6776
  • View blog
  • Posts: 27,942
  • Joined: 12-December 12

Re: Using fgets() to read 2 lines from .cfg file

Posted 14 January 2019 - 01:57 AM

Question moved out of Discussion Lounge. You are not introducing a discussion topic but asking a question about your code and its issues.
Was This Post Helpful? 0
  • +
  • -

#4 aemaeth   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 10-February 17

Re: Using fgets() to read 2 lines from .cfg file

Posted 15 January 2019 - 06:56 PM

Thank you.

What if you didn't know how many lines you were reading from a file, like a .txt and had to store the input into variables? Would you need to make an "if" block for every line? Is fscanf() better?

I was hoping for there to be some better method to read input files, like in Java, where you can use a String Tokenizer or string split to read each line of an input file and simply use a while loop or for loop to read it until the last line.

Thank you, for now the fgets() will suffice.

This post has been edited by Skydiver: 16 January 2019 - 11:16 AM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6821
  • View blog
  • Posts: 23,201
  • Joined: 05-May 12

Re: Using fgets() to read 2 lines from .cfg file

Posted 16 January 2019 - 12:51 AM

Your basic approach in your original post is incorrect. It would not have worked in Java or C. In pseudo code you had:
open file
while (successfully read a line from the file)
{
   prec = parse integer from line
   cols = parse integer from line
}


I don't see how that would have magically worked in Java, but fail to work in C.

As for you question about a string tokenizer, strtok() is part of the C standard library. There is also nothing preventing you from writing your own tokenizer. You could also use sscanf() as an alternative.

View Postaemaeth, on 15 January 2019 - 08:56 PM, said:

What if you didn't know how many lines you were reading from a file, like a .txt and had to store the input into variables?

Just like in Java, if you don't know the number of lines ahead of time, there is no way to store the input in variables because you would not know how many variables to declare. What you would do, just like Java, is use a dynamically growing data structure to store the values.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1