file IO, again i know!!

This is a discussion on file IO, again i know!! within the C Programming forums, part of the General Programming Boards category; Hi, Firstly apologies for the long post. I just want to try to make clear what i'm doing. i'm trying ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    2

    file IO, again i know!!

    Hi,

    Firstly apologies for the long post. I just want to try to make clear what i'm doing. i'm trying to read in values from two files then perform an operation on them later to ouput to another file. The files i'm reading from have data like this look like this :

    Packet No.: 1
    system_time.wHour: 18
    system_time.wMinute: 31
    system_time.wSecond: 21
    system_time.wMilliseconds: 794
    Bytes Sent: 21

    The code i've developed so far deals with reading line by line, performing the required operations i.e. malloc and atoi. I'm not certain i'm attacking this the right way though the only error i get is with getline. Have tried to use fgets, fgetc and others. From reading through (many) posts i gather that i might read line using fgets and then parse it using sscanf. Is there a way i can use getline/fgetline? At the moment the error says that fgetline can't take 3 parameters. Should i make wholesale changes to code and try a different method? Any help would be appreciated, it's been driving me crazy.

    Here's a sample of some of the code:

    Code:
    :
    
    int main()
    {
      int bytes_read;
      int nbytes = 100;
    
      char *my_string1;
      char *my_string2;
      char *my_string3; //etc
    ........
    .......
    FILE * fp1;
    int packetNo[1000];
     int time[1000];
    int counter = 0;
    
    
    fp1 = fopen("c:\\client.txt","r");
    
      my_string1 = (char *) malloc (nbytes + 1);
      my_string2 = (char *) malloc (nbytes + 1);
      my_string3 = (char *) malloc (nbytes + 1);
    
    while (1) {
      bytes_read = getline (&my_string1, &nbytes, fp1);
      bytes_read = getline (&my_string2, &nbytes, fp1);
      bytes_read = getline (&my_string3, &nbytes, fp1);
    
    packetNo[counter] = atoi(&my_string1[12]); 
    
      time[counter] = atoi(&my_string3[12]) * 1000 + atoi(&my_string4[20]);
     
       counter++;
    	
      }
    
    fclose (fp1);
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    getline is a C++ function.

    Here's an example of reading a file and parsing the number from each line.

    Code:
    #include <stdio.h>
    
    #define MAXLINE 80
    
    int main(void)
    {
        FILE *fp;
        char line[MAXLINE];
        int num;
    
        fp = fopen("client.txt","r");
    
        while(fgets(line, MAXLINE, fp)) {
            printf("%s", line);
            if(sscanf(line, "%*[^0123456789]%d\n", &num) == 1) {
                printf("%d\n", num);
            }
        }
    
        return 0;
    }
    Other stuff:
    You don't need to (shouldn't) cast malloc. Explanation in the FAQ
    You should check that your file pointer is not null after fopen
    You should free all memory that you malloc'd

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,075
    He's referring to the getline in stdio.h. Which is only in GNU's version of stdio.h and is (even though it's actually pretty good) not standard. You should use fgets as people suggest.
    Sent from my iPadŽ

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    2
    Hi again,

    Sorry for the late reply, haven't been at computer since i posted earlier. Thanks for the advice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04:30 PM
  5. File IO with .Net SDK and platform SDK
    By AtomRiot in forum Windows Programming
    Replies: 5
    Last Post: 12-14-2004, 10:18 AM

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