2

I need to read some numbers from a file such that I dont have any information about how many numbers are in an specified line, however the number of lines is given in the file, as shown below, where the first line give me the number of lines. I need some tip on how I cant read these numbers to keep them in integer format (not characters)

10
1 2 3 4 5
2 3 4
3 4
4 5 6
5
6 7 10
7 8 9
8 9
9
10
jxh
  • 69,070
  • 8
  • 110
  • 193
Fabricio
  • 31
  • 1
  • Use a loop, and check when you reached the end of the file. – Kerrek SB Oct 09 '13 at 19:03
  • 1
    You run a loop from 0 to `n`, read a line, convert to integer. –  Oct 09 '13 at 19:03
  • How I can check end of line when I reading integers? With characters I can verify this when the character is equal '\n'. – Fabricio Oct 09 '13 at 19:11
  • Agree with other comments, but will add that the "numbers" if stored in a file, are stored as strings already, so regarding your desire to _read these numbers to keep them in integer format_, you cannot. You have to read and parse them as strings using `strtok();`, then _convert_ back to integers using something like `atoi();` or `atol();` – ryyker Oct 09 '13 at 19:12
  • Is the line number count always the first number (except for the first line, which just gives the number of lines)? – jxh Oct 09 '13 at 19:13
  • with characters I have some problems using the function "atoi" and "strtok()", in the output appear some "zeros" and in the file dont have any zero as you can see. Maybe I will try this function atol() – Fabricio Oct 09 '13 at 19:17
  • Yes. the first number of the line always indicates the current line – Fabricio Oct 09 '13 at 19:20
  • possible duplicate of [reading unknown number of integers from stdin (C)](http://stackoverflow.com/questions/2195823/reading-unknown-number-of-integers-from-stdin-c) – UpAndAdam Oct 09 '13 at 19:21
  • 1 Use first number to create VLA or `malloc(N*sizeof number)`. 2 Use a loop with `char buf[N * 20]; fgets(buf, sizeof buf, stdin)`. 3 nested loop using sscanf(%d) or strtol() to parse the `buf`. – chux - Reinstate Monica Oct 09 '13 at 19:21
  • 1
    _How I can check end of line when I reading integers?_ fgets() will read each line of a file until EOF is reached, then pass a NULL. You are not reading integers, you are reading a line, or a string, null terminated, containing values that look like numbers but are really just ASCII characters. Some of them are used to represent numbers. [ASCII table](http://web.cs.mun.ca/~michael/c/ascii-table.html). – ryyker Oct 09 '13 at 19:21
  • My original answer had some errors that I have corrected, i.e. passing `\n` to `atoi()` at the end of each line resulted in zeros being placed into `numbers`, so I added "\n" as a delimiter to `strtok()` functions. – ryyker Oct 10 '13 at 14:38

1 Answers1

-1

Here is a simple example using fgets, strtok and atoi. (since your input only uses small ints, atoi will work fine.) Be sure to edit the file path at the top.
Note: this is just a quick and dirty example, i.e. numbers is hardcoded to only hold 100 values, the line buf is hardcoded to only hold a line 259 characters + 1, etc, etc. If you want to make it more flexible (bigger files, more numbers), use calloc(); and free(); to create and free memory as you need it.

#include <ansi_c.h>
int main(void)
{
    FILE *fp;
    char *tok;
    int numbers[100];
    char tempBuf[5], lineBuf[260];
    int i=0;

    memset(numbers, 0, sizeof(numbers));

    fp = fopen("C:\\dev\\play\\numbers.txt", "r");
    while(fgets (lineBuf, sizeof(lineBuf), fp))
    {
        tok = strtok(lineBuf, " \n");
        while(tok)
        {
            strcpy(tempBuf, tok);
            numbers[++i] = atoi(tempBuf);
            tok = strtok(NULL, " \n");
        }
    }
    fclose(fp);

}
ryyker
  • 22,849
  • 3
  • 43
  • 87