2

I want to be able to make a loop that reads line by line, then captures the digits in the beginning of each line into an int array, and the characters in a 2d character array. I thought I could have a loops like,

while (fscanf(file, "%d %c %c %c", &num, &f, &e, &h)==4){}

but that is if C could read strings. How can I read each line?

  • you should be able to manage reading until a buffer is full, then `realloc`ing the buffer to make it twice as big, then reading to the second half of the buffer, and repeatedly doing this until you've read the whole line. – Wug Oct 23 '12 at 20:07
  • The second parameter is not a character. So `%c` will fail your scanning. Use `%s` instead. – Shiplu Mokaddim Oct 23 '12 at 20:08

3 Answers3

1

For reading a line you can use :-

  while ( fgets ( line, sizeof line, file ) != NULL )

or you can try

  while ((read = getline(&line, &len, fp)) != -1)
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0
      #include <stdio.h>
      #include <stdlib.h>
      int main()
      {
          char matrix[500][500], space;
          int numbers[500], i = 0, j;
          FILE *fp = fopen("input.txt", "r");

          while(!feof(fp))
          {
                fscanf(fp, "%d", &numbers[i]); // getting the number at the beggining
                fscanf(fp, "%c", &space); // getting the empty space after the number
                fgets(matrix[i++], 500, fp); //getting the string after a number and incrementing the counter
          }
        for(j = 0; j < i; j++)
            printf("%d %s\n", numbers[j], matrix[j]);
      } 

variable 'i' is counting how many lines you have. If you have more than 500 lines you can change that value or then use a dynamic vector.

vmp
  • 2,370
  • 1
  • 13
  • 17
  • I believe doing it this way was exactly what OP wanted to avoid. – Wug Oct 23 '12 at 20:11
  • matrix[500][500] is an array of strings, 500 strings of size 499 each. numbers[500] is an array of integers... For example, if you want the 30th line in the file, you can get the number for that line in number[29] and the string for that line in matrix[29] – vmp Oct 23 '12 at 20:23
  • copy the code and paste it, then compile and create a file with name input.txt and save it in the same directory. Then what you wanted in that file: 31 Jaadds D d 33 DdFfadds D d 32 dfdsfdsfFff d a and rune the code to see what happens. the data was loaded into matrix and numbers – vmp Oct 23 '12 at 20:29
0

what about this:

    char buf[512];
    int  length = 0;

    while(fgets(&buf[0], sizeof(buf), stdin)) {
        length = strlen(&buf[0]);
        if(buf[length-1] == '\n')
            break
        /* ...
        ... 
        realloc or copy the data inside buffer elsewhere.
        ...*/
    }

/* ... */
yeyo
  • 2,954
  • 2
  • 29
  • 40
  • getline is your best option, but is not portable, POSIX. – yeyo Oct 23 '12 at 20:17
  • `&buf[0]` is better written as just `buf` – William Morris Oct 23 '12 at 21:45
  • yes, but I made a habit of it, I always try to be as explicit as possible even if it's pointless. What can I say? as human this may be one of many of the mistakes I make, however, you are right and thanks for pointing this out. – yeyo Oct 25 '12 at 06:09