2009-11-01 4 views
4

Je souhaite charger un nombre connu de nombres dans un tableau en C à partir d'un fichier texte (.txt). Le format serait:Charger des nombres à partir d'un fichier texte dans C

« 0,1,2,5,4 »

Je suis un peu nouveau à C, peut-on recommander un moyen de charger dans le fichier texte?

Vive

+0

devoirs peut-être? –

Répondre

5

Il peut se faire facilement avec 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: Bien sûr, pourquoi pas. Je viens que si elle est un hw, il est une mauvaise chose pour donner une solution complète :)

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

La dernière entrée manquera. –

+0

@Daniel: testez-le! – pmg

+0

@Arak: changez votre exemple pour remplir un tableau: '... while() {arr [index ++] = nombre;} ...' – pmg

2

Vous pouvez:

1) Lire un numéro à la fois et convertir en int en utilisant atoi()

2) Vous pouvez lire le tableau tout à la fois et en utilisant strtok à diviser le nombre et après que convertir avec atoi()

ici un exemple de strtok:

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

Hy essayez ceci.

#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

Assurez-vous toujours ce que vous lisez de la valeur. Si vous lisez des caractères du fichier ok. Mais si vous voulez lire des entiers, assurez-vous toujours de les lire comme des caractères et de les convertir en entiers.

#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]); 
} 

ceci est mon fichier texte,

3210 

c'est ma sortie,

3 
2 
1 
0 
Questions connexes