0

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.

5
  • What did you get? Did your file contain a space after each value? You should also add checking that fopen() and fscanf() worked. Commented Sep 16, 2013 at 6:19
  • c++? or c? This looks like the latter. The former tag should probably be removed. Commented Sep 16, 2013 at 6:20
  • Add your file content and your out put in question. Commented Sep 16, 2013 at 6:25
  • possible duplicate of Load numbers from text file in C Commented Sep 16, 2013 at 6:27
  • This is a duplicate of stackoverflow.com/questions/1658530/… Commented Sep 16, 2013 at 7:30

5 Answers 5

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0
#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

2 Comments

This program just prints the first value in the text file- 3. This doesnt print the other values present in the txt file.Any solution?
err...how did you run the program? I have a text file here with 4 numbers and this program still prints all 4 numbers
0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.