Thread: Reading data from a file till there is a newline?

  1. #16
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAXSIZE 150
    
    int main (void)
    {
      /* By using variables starting with "str" the
      ** C standard doesn't guarantee your program to
      ** work at any time.
      */
      char fstr[MAXSIZE];
      FILE *infp;
      int count = 0;
      /* perror is specially designed to report errors
      ** after a library function fails. It gives more
      ** information than a homegrown error report in
      ** most cases and is easier to use. Also, 1 is
      ** a non-standard exit condition for main, the
      ** three portable values are 0, EXIT_SUCCESS, and
      ** EXIT_FAILURE. The last two are found in stdlib.h.
      */
      if((infp = fopen("input.txt", "rt"))== NULL) {
        perror("Unable to open input file");
        exit(EXIT_FAILURE);
      }
      printf("\nNow counting number of records on file");
      /* When reading lines you don't want to use scanf,
      ** that isn't what it is designed for. fgets on the
      ** other hand is very well suited to this task and
      ** results in shorter code if you've noticed.
      */
      while(fgets(fstr, sizeof fstr, infp)!= NULL)
        count++;
    
      printf("\n\nNumber of records on file is %d", count);
      getchar();
      fclose(infp);
      /* Personal preference: If I use EXIT_FAILURE 
      ** anywhere in my programs, I'll use the corresponding 
      ** macro EXIT_SUCCESS when I return from main. It just
      ** looks neater.
      */
      return EXIT_SUCCESS;
    }
    >For example, "one" is a word and "meaning of one" is a
    >meaning of one. How would I divide each line into two pieces so
    >I can have word and meaning separately?
    The easiest way is to use a delimiting character which is not in the data itself to divide the line. This way you can use sscanf, strtok, and similar functions to break up the string:

    one : meaning of one
    Code:
    if ( fgets ( a, sizeof a, file ) != NULL )
        sscanf ( a, "%s : %[^\n]", b, c );
    It's also usually a good idea to double check that sscanf executed properly so that you can work out errors as close to the source as possible.

    -Prelude
    My best code is written with the delete key.

  2. #17
    Registered User TravisS's Avatar
    Join Date
    Jun 2002
    Posts
    536
    Originally posted by quzah


    Um... no. This is entirely wrong. What is this even supposed to do? First off, 'str' is an array, and you're dereferencing it ... and then you try to use .... yeah. Anyway it's wrong.

    Quzah.
    strch looks at a string (aka charachter array) and tries to find a certain character in that array. In this case, the character it's looking for is a '\n'.

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by TravisS


    strch looks at a string (aka charachter array) and tries to find a certain character in that array. In this case, the character it's looking for is a '\n'.
    Yeah I know exactly how 'strchr' works. You are using it wrong. Let me explain:

    Code:
    int main (void)
    {
    
      char str[150];
       /* 'str' is an array. */
    
    
      FILE  *infp;
      int count = 0;
    
      if((infp = fopen("myfile.dat", "rt"))== NULL) {
    		printf("\nE R R O R : Unable to open input file");
    		exit (1);
      }
      printf("\nNow counting number of records on file");
    
       /* file is open, that's fine */
    
       /* What are you doing here? */
      while((*str = fgetc(infp))!= EOF) {
    
               
    			/* why do this? */
    			if( strch(str, '\n') )
    			  count++;
    			}
      
      printf("\n\nNumber of records on file is %d", count);
      getchar();
      fclose(infp);
      return 0;
    }
    There is no point at all to use an array in your code. There is no point in using strchr (which it incidently called, there is no ansi function called 'strch').

    All you ever do is use the first element of your array. Since that's the case, why bother using an array?

    Furthermore, your example won't work because the fgetc call returns an integer, which is typecast into a 'char', so it will never end up returning EOF.

    All you need, to do what you're attempting, is:
    Code:
    int c, count=0;
    while( (c=fgetc(fp))!=EOF )
    {
        if( c == '\n' ) count++;
    }
    That's all. Nothing else. No other function calls. No arrays. Your example wouldn't work right, assuming you had your function named correctly ('ll chalk that up as a typo).

    Anyway, there's no point in using an array for the method you use, and your return value (of fgetc) would never evaulate as true in your loop.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #19
    Registered User TravisS's Avatar
    Join Date
    Jun 2002
    Posts
    536


    my bad

    Yeah, I meant strchr, and no, I didn't look at what I was typing

    You're absolutely right, what was there made no sense at all. All I did was throw in some code without changing what was already there, so it ended up looking weird.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding reading data from file into arrays
    By vutek0328 in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 09:20 AM
  2. reading in file data
    By sickofc in forum C Programming
    Replies: 2
    Last Post: 03-01-2006, 05:16 PM
  3. reading data into a queue from file
    By bcianfrocca in forum C++ Programming
    Replies: 12
    Last Post: 11-17-2005, 11:28 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. reading data from a file
    By brianptodd in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2003, 06:50 PM