fgets help

This is a discussion on fgets help within the C Programming forums, part of the General Programming Boards category; Hi Can someone point me in the correct direction here I want to use fgets to read in a stream ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    39

    fgets help

    Hi

    Can someone point me in the correct direction here

    I want to use fgets to read in a stream of data from a text file

    I call fgets once and it reads the first line and stores it in my string.
    If i call fgets again it does read the next line but it writes over the information in the array stored from the first call of fgets

    How can i get it to start storing data in my array where it left off?

    Thanks so much

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    
    
    int main( int argc, char *argv[] )
    {
      FILE *fptr;
    
      int count_line=0;
      char temp_array[100];
      char *temp_string;
      temp_string=&temp_array[0];
      int x;
    
    
    //*****************************************************************************************************************//
    for(x=0;x<100;x++){
    
    temp_array[x]='\0';
    
    
    }
    
    //*****************************************************************************************************************//
    
    
      if (argc != 2) {
        fprintf (stderr, "Error: The program is run with 1 input argument");
        exit(-1);
      }
    
      // Open file and check it is open
      fptr = fopen(argv[1], "r");
      if (fptr == NULL) {
        fprintf (stderr, "Error: Could not open file");
        exit(-1);
      }
    
    //*****************************************************************************************************************//
    
    //load the array up with first line of characters
    //This is also going to handle the line count
    
    while(!feof(fptr)){
    
    fgets(temp_string,100,fptr);
    
    count_line++;
    }
    
    //*****************************************************************************************************************//
    
    
    
    for(x=0;x<10;x++){
    
    printf("This is what is stored in array location %d  ""%c""\n\n",x,temp_array[x]);
    
    
    }
    
    fprintf(stderr,"This is the count %d\n\n\n\n",count_line-1);//Gedit in linux is adding an addtional line character. The minus one accounts for that.
    //different systems may behave differently.
    
    
    
    
    
      fclose (fptr);
    
    }

  2. #2
    Registered User
    Join Date
    Jan 2012
    Posts
    39
    Nevermind, switched to fread and now does what i need

    Thanks if i knew how to delete the post i would

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by automagp68 View Post
    If i call fgets again it does read the next line but it writes over the information in the array stored from the first call of fgets
    That's because you're storing it all in the same place. If you want the next call to go somewhere else, put it somewhere else. Switching to fread isn't going to change that.


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

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    39
    Quote Originally Posted by quzah View Post
    That's because you're storing it all in the same place. If you want the next call to go somewhere else, put it somewhere else. Switching to fread isn't going to change that.


    Quzah.

    fread did exactly as i wanted i wanted the whole file in an array thats it.

    but if i use fgets when it goes to read line two it over writes line one see what i mean.

    so if i use fread it does it all at once the whole thing.

    So it just fills the array with all the data

    Do you know of a way to make fgets start stroring in the same array where it left from the previous call?

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    Whatever you think your code is doing, you're wrong. No mere switching to fread is going to make much difference as to the correctness of your program.

    You don't have an array to store the contents of any file larger than 100 characters.
    You're using feof wrong - read the FAQ.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets
    By mesmer in forum C Programming
    Replies: 1
    Last Post: 11-24-2008, 11:37 PM
  2. using fgets()
    By rambos in forum C Programming
    Replies: 5
    Last Post: 05-04-2008, 11:39 PM
  3. some help on fgets()
    By pothios in forum C Programming
    Replies: 3
    Last Post: 03-14-2008, 06:49 AM
  4. Help using fgets()
    By ICool in forum C Programming
    Replies: 7
    Last Post: 09-10-2007, 06:24 AM
  5. fgets
    By G'n'R in forum C Programming
    Replies: 8
    Last Post: 09-18-2003, 12:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21