Thread: Reading Lines from File Issue (integers and character strings)

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    26

    Reading Lines from File Issue (integers and character strings)

    I'm trying to find a way to input data from a file, line by line, into two separate arrays.

    Say I have a file, input.txt:
    3 hp psc 2210
    0 inkjet
    100 laserjet

    I need to first, get the number, then the title. I want to put them into 2 different arrays.
    I can easily get the pivot point by...
    Code:
    int c;
    int count = 0;
    char line [ 128 ];
    FILE * file;
    file = fopen ( "input.txt", "r" );
    while(count < numLinesInTextFile) // pseudo code to iterate as many times as there are lines
    {
    	while(c = getc(file) != ' ')
    	{
    		// this will iterate through the line until we get past the initial amount number 
    		// 3 for the first line, 0 for second, and 100 for third
    	}
    	fgets(line, sizeof line, file); // this will get the title after the initial number
                                                      // line will hold the string
    }
    Say there are two arrays, initialized with the size of the the number of lines, in the example case, 3
    amountArray[numLines];
    titleArray[numLines];

    I'm having difficulties putting the necessary values into each array.
    After the code executes, the values in amountArray should be...
    amountArray[0] = 3,
    amountArray[1] = 0,
    amountArray[2] = 100.

    while titleArray values should be...
    titleArray[0] = 'hp psc 2210'
    titleArray[1] = 'inkjet'
    titleArray[2] = 'laserjet'

    What is the appropriate way of doing this? Is there a better way I could be doing this?

    thank you for any help

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    fgets( fp, buf, BUFSIZ );
    sscanf( "%d %[^\n]", num[ x ], words[ x ] );

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

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    26
    I'm getting multiple errors with attempting to use that method. Should the fgets function be in the format of
    Code:
    char buf[100];
    int BUFSIZ = 100;
    fgets(buf, BUFSIZ, fp);
    and should the sscanf have the buf as the first argument?
    Code:
    int num[3];
    char words[3];
    sscanf(buf, "%d %[^\n]", num[ x ], words[ x ] );
    If I try and only do this one time (for the first line) and set x = 0, num[0] should hold 3 and words[0] should hold 'hp psc 2210'

    Is this correct?

    complete code:
    Code:
    int num[3];
    char words[3];
    char buf[100];
    int BUFSIZ = 100;
    fgets(buf, BUFSIZ, fp);
    sscanf(buf, "%d %[^\n]", num[ x ], words[ x ] );
    When I try and run this code, I get a segmentation fault. What may I be doing wrong?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I see that extra space around the x's. I don't know what that might/might not cause. In general scanf() family of functions are very finicky - you have to use them "just right". Yes, sscanf() needs the name of the string, which is the address as well, to find the string being scan'ed.

    %[whatever] has always given me headaches in scanf()

    Here's my take on your problem:
    Code:
    #include <stdio.h>
    
    
    int main(void) {
      FILE *fp;
      void* neof; 
      int i, num;
      int numbers[3];
      char buff[100], ch;
    
      if((fp = fopen("input.txt", "rt")) == NULL)
        return 1;
      printf("\n\n");
      i = 0;
      do {
        if(i < 3) 
          fscanf(fp, "%d%c", &num, &ch);
    
        numbers[i++] = num;
        neof = fgets(buff, sizeof(buff), fp);
        if(neof)
          printf("\nNumber: %d Buff: %s", num, buff);
    
      }while(neof);
      fclose(fp);
    
      for(i = 0; i < 3; i++)
        printf("\n Numbers[%d]: %d", i, numbers[i]);
      printf("\n\n\t\t\t     press enter when ready");
      i = getchar(); 
      return 0;
    }
    Last edited by Adak; 10-17-2009 at 10:20 AM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Adak View Post
    I see that extra space around the x's.
    No, whitespace arround array indexing doesn't matter. Just like whitespace around ... well anything ... doesn't matter. Whitespace outside of strings never matters. (Possible exception being #<preprocessor directive> statements that say it should be the first character on a line. But it's the same as:
    Code:
    void foo(type bar);
    void foo ( type bar );
    None of that whitespace matters.

    Yes, I did miss out on putting the buffer first. I forgot in my changing between using fscanf and fgets+sscanf.

    To OP:
    Code:
    char words[3];
    That didn't work because you have an array of three characters, not an array of three words (strings).


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Reading from File
    By Bnchs in forum C Programming
    Replies: 8
    Last Post: 12-09-2007, 03:17 PM
  3. Reading from a file into a linked list.
    By Wiretron in forum C Programming
    Replies: 5
    Last Post: 01-23-2006, 08:24 AM
  4. Scanning integers from a file:
    By xarmenx in forum C Programming
    Replies: 6
    Last Post: 11-29-2004, 04:57 PM
  5. reading string and integers from a text file
    By Gator in forum C Programming
    Replies: 2
    Last Post: 11-25-2001, 02:17 PM