Load numbers from a text file in C

I want to load a known number of numbers into an array from C from a text file (.txt). The format will be:

"0,1,2,5,4"

I'm new to C, can anyone recommend a way to load into a text file?

Greetings

+4
source share
4 answers

This can be done easily with fscanf :

 #include <stdio.h> int main() { FILE* f = fopen("test.txt", "r"); int number = 0; int sum = 0; /* the sum of numbers in the file */ while( fscanf(f, "%d,", &number) > 0 ) // parse %d followed by ',' { sum += number; // instead of sum you could put your numbers in an array } fclose(f); } 

@pmg: Of course, why not. I just, if it is hw, then it is bad to give a complete solution :)

 #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); } 
+5
source

You can:

1) Read one number at a time and convert to int using atoi ()

2) You can read the entire array at once and use strtok to divide the number and then convert with atoi ()

Here is a strtok example:

  int main(int argc, char *argv[]) { int x = 1; char str[]="this:is:a:test:of:string:tokenizing"; char *str1; /* print what we have so far */ printf("String: %s\n", str); /* extract first string from string sequence */ str1 = strtok(str, ":"); /* print first string after tokenized */ printf("%i: %s\n", x, str1); /* loop until finishied */ while (1) { /* extract string from string sequence */ str1 = strtok(NULL, ":"); /* check if there is nothing else to extract */ if (str1 == NULL) { printf("Tokenizing complete\n"); exit(0); } /* print string after tokenized */ printf("%i: %s\n", x, str1); x++; } return 0; 
+2
source

Hy try this.

 #include <stdio.h> #define MAX_NUMBERS 1000 /* Max numbers in file */ const char DATA_FILE[] = "numbers.dat"; /* File with numbers */ int data[MAX_NUMBERS]; /* Array of numbers */ int main() { FILE *in_file; /* Input file */ int middle; /* Middle of our search range */ int low, high; /* Upper/lower bound */ int search; /* number to search for */ char line[80]; /* Input line */ in_file = fopen(DATA_FILE, "r"); if (in_file == NULL) { fprintf(stderr,"Error:Unable to open %s\n", DATA_FILE); exit (8); } /* * Read in data */ max_count = 0; while (1) { if (fgets(line, sizeof(line), in_file) == NULL) break; /* convert number */ sscanf(line, "%d", &data[max_count]); ++max_count; return data; } return (0); } 
0
source

Always check what you read from the value. If you read the characters from the file in order. But if you want to read integers, always check 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); } fclose(point); // 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 conclusion

 3 2 1 0 
0
source

All Articles