0

120 test 123

101 test1 132

126 test2 140

150 test3 150

This is inside of a txt file. What i want to do is getting only the first number in the first line of the file and put it into an array and go to the next line then do the same thing again... for example array[0]={120} , array[1]={101}...

int j,i=1,array[20];
FILE *fp;

fp=fopen("input.txt","r");

for(i=0;i<10;i++)
    fscanf(fp,"%d",&array[i]); 

This is what i tried to do but i guess fscanf doesnt skip the line after getting the number... It might be a newbie question but i am stuck on that part :P

  • 3
    You need to consume a LINE of data with each read. Read each line into a character array (buffer) with `fgets()`, then use `sscanf (buffer, "%d", &array[i])` to store the first number in each line to an array. – David C. Rankin Apr 11 '20 at 06:38

2 Answers2

1

Continuing from the comment, you want to consume an entire line of input with each read, then parse the first integer from the line into your array. Using fgets() to read each line into a buffer and then parse with sscanf is a simple solution, e.g.

#include <stdio.h>

#define MAXC 1024   /* if you need a constant, #define one (or more) */
#define MAXI  256

int main (int argc, char **argv) {

    char buf[MAXC];
    int array[MAXI], n = 0;
    /* use filename provided as 1st argument (stdin by default) */
    FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;

    if (!fp) {  /* validate file open for reading */
        perror ("file open failed");
        return 1;
    }

    while (n < MAXI && fgets (buf, MAXC, fp))     /* read each line into buf */
        if (sscanf (buf, "%d", &array[n]) == 1)   /* parse 1st int from buf to array */
            n++;                                  /* increment counter */

    if (fp != stdin)   /* close file if not stdin */
        fclose (fp);

    for (int i = 0; i < n; i++)
        printf ("array[%2d] : %d\n", i, array[i]);
}

Example Input File

$ cat dat/arr_1st_num.txt
120 test 123

101 test1 132

126 test2 140

150 test3 150

(note: blank lines or lines not beginning with an integer value are simply read and discarded and do not impact your read and storage of data)

Example Use/Output

$ ./bin/array_1st_num dat/arr_1st_num.txt
array[ 0] : 120
array[ 1] : 101
array[ 2] : 126
array[ 3] : 150

Look things over and let me know if you have further questions.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
0

What you can do is you can scan the whole line instead as a string, then you get the first token (which is the first number) by getting the first "token" of the string using the strtok function. Lastly, we use the atoi function to convert the string number to an integer number (don't forget to include stdlib.h)

#include<stdlib.h>

int j, i=1, array[20];
FILE *fp;

fp = fopen("input.txt","r");

char currentLine[1000];
for(i=0; i<10; i++) {
   fscanf(fp, "%[^\n]", currentLine);

   // extract the first token
   char * firstToken = strtok(currentLine, " ");

   // store the firstToken (which is the number)
   // to the array
   array[i] = atoi(firstToken);
}
Jude Maranga
  • 865
  • 2
  • 9
  • 27