Force.comBat
Force.comBat

Reputation: 245

Read an input file to an array

How can I read a text file's content and put it into an array? For example, I have 3, 2, 1, 0 in my text file and I want to read the file and store the values in an array. I am using the fscanf function to do this:

int a[4];
point = fopen("test.txt", "r");

for(int i = 0; i < 4; i++)
{
    fscanf( point , "%d " , &a[i]);              
}

// printing put the values ,but i dont get the text file values

for(int i = 0; i < 4; i++)
{
    printf("%d\n" , a[i]);  
}

I ran this program but I didn't get the values present in the text file. Can anybody please suggest a way to do this? I want to specifically solve it with the fscan function.

Upvotes: 0

Views: 16088

Answers (5)

Karthik Sivam
Karthik Sivam

Reputation: 43

First you should know the order in which the numbers are written in the text file, if they are separated by a single space, you can use your code like this:

for(int i=0; i<4; i++)
{
fscanf(point, "%d ", &a[i]);
}

You should leave a space after %d. If numbers are written in separate lines, you can use your code like this:

 for(int i=0; i<4; i++)
{
fscanf(point, "%d\n", &a[i]);
}

That's all, you can print those values as your wish.

Upvotes: 0

Guangming Mao
Guangming Mao

Reputation: 775

fscanf if used for reading data from the stream and storing them according to the parameter format into the specified locations. You can get the reference here.

So you must check the values' format in the file, for your example, you have "3,2,1,0" in the file, you should set the format to "%d," because each value was followed a ','.

#include <stdio.h>

int main()
{
    int a[4], i;
    FILE *point = fopen("test.txt", "r");

    for(i = 0; i < 4; i++)
    {
        fscanf( point , "%d," , &a[i]);
    }

    // printing put the values ,but i dont get the text file values

    for(i = 0; i < 4; i++)
    {
        printf("%d\n" , a[i]);
    }
}

I test it with my codeblocks on windows, I get

3
2
1
0

Upvotes: 1

Megharaj
Megharaj

Reputation: 1619

Always make sure what you are reading from the value. If you are reading characters from the file ok. But if you want to read integers always make sure you read them as characters and convert them to integers.

#include<stdio.h>
int main()
{
    char a;
    FILE *point;
    int i, b[4];
    point = fopen("test.txt", "r");
    for(i = 0; i < 4; i++) {
            a = fgetc( point); 
        b[i] = atoi(&a);              
    }
// printing put the values ,but i dont get the text file values
    for(i = 0; i < 4; i++) 
        printf("%d\n" , b[i]);  
}

this is my text file,

3210

this is my output,

3
2
1
0

Upvotes: 0

smac89
smac89

Reputation: 43068

#include <stdio.h>
#include <stdlib.h>

void main() {
    int a[4];
    FILE* point = fopen("test.txt", "r");

    if (NULL == point) {
        perror("File not found!");
        exit(-1);
    }

    for (int i = 0; fscanf(point, "%d%*c", &a[i]) > 0 && i < 4; i++) {
        printf("%d\n", a[i]);
    }

    fclose(point);
}

test.txt:

11234, 2234, 32345, 42542

Upvotes: 0

No Idea For Name
No Idea For Name

Reputation: 11577

you can find your answer here:

#include <stdio.h>
int main()
{
    FILE* f = fopen("test.txt", "r");
    int n = 0, i = 0;
    int numbers[5]; // assuming there are only 5 numbers in the file

    while( fscanf(f, "%d,", &n) > 0 ) // parse %d followed by ','
    {
        numbers[i++] = n;
    }

    fclose(f);
}

Upvotes: 1

Related Questions