Reading in sourcelines from a file

This is a discussion on Reading in sourcelines from a file within the C Programming forums, part of the General Programming Boards category; Hey there, I'm trying to read in sourcelines from a file and save them to an array. I want to ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    61

    Reading in sourcelines from a file

    Hey there, I'm trying to read in sourcelines from a file and save them to an array. I want to skip any lines that contain only whitespace first though. Here's what I've got so far but I get an incompatible pointer type error. I always have issues with pointers lol

    Code:
    /* Pre-Processor Directives */
    #include <stdio.h>
    #include <string.h>
    #include "cop3601.h"
    
    extern char sourcelist[][81];
    
    int getsource(char filename[])
    {
       int lines = 0;
       char ws[] = " \t";
       FILE *pFile;
    
       if((pFile = fopen(filename,"r")) != NULL)
       {
          fgets(sourcelist, 81, pFile);
          strtok(sourcelist, "\n");
       }
       else
          hprintf("\nFile %s does not exist.\n\n");
    
       fclose(pFile);
    
       return lines;
    }
    I'm sure it's something really simple that I'm screwing up, but any ideas are helpful!
    Last edited by williamsonj; 04-07-2010 at 10:23 PM.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    sourcelist is declared as an array of strings from what I gather and fgets takes a string as the first parameter.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    Right, I'm trying to save each line as a string in my array, each index in the array holds a line from the file

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by williamsonj View Post
    Hey there, I'm trying to read in sourcelines from a file and save them to an array. I want to skip any lines that contain only whitespace first though. Here's what I've got so far but I get an incompatible pointer type error. I always have issues with pointers lol

    Code:
    /* Pre-Processor Directives */
    #include <stdio.h>
    #include <string.h>
    #include "cop3601.h"
    
    extern char sourcelist[][81];
    
    int getsource(char filename[])
    {
       int lines = 0;
    /* ws needs a size. "\t" looks like the char '\t' but is actually a string with two chars:
    \t\0. EVERY string MUST have an end of string char appended to it, or it's just a
    bunch of letters - and not a string.
    */
    
       char ws[] = " \t";   //give it a size of 2, at least
       FILE *pFile;
    
       if((pFile = fopen(filename,"r")) != NULL)
       {
           You need a while loop to start here, with fgets(... etc), inside the testing loop
    
          fgets(sourcelist, 81, pFile); //you need an index number for which row: sourcelist[i]
          strtok(sourcelist, "\n");
       }
       else
          hprintf("\nFile %s does not exist.\n\n");
    
       fclose(pFile);
    
       return lines;
    }
    I'm sure it's something really simple that I'm screwing up, but any ideas are helpful!
    hprintf(...etc.), needs to be just printf(...etc).

    Good looking code, though.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    hprintf is a function included in the cop3601.h file

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by williamsonj View Post
    Right, I'm trying to save each line as a string in my array, each index in the array holds a line from the file
    Then you should call fgets on that LINE:

    Code:
    int i= 3; /* read line 3*/
    fgets(sourcelist[i],81,stdin);
    Also, how many lines is sourcelist supposed to contain? Is memory allocated statically or dynamically for these lines?
    Last edited by claudiu; 04-07-2010 at 11:21 PM.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    sourcelist will be set in a driving class and will most likely be done dynamically

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ok, well the incompatible pointer type is because you are passing sourcelist (i.e. char**) to fgets instead of sourcelist[i] (i.e char*).

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    61
    OK, thanks, I'll try making those changes. I'm not 100% sure how the sourcelist array size gets set so I'll have to figure that out first.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

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