2015-04-24 2 views
-6

Я пытаюсь получить данные из quiz_scores.txt для печати на экран, чтобы я мог сканировать его в массив, но я не знаю, как это сделать. Нужно ли мне жестко закодировать файл в моей программе, и если да, то как?Как получить файл для печати на экран?

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

int main(void) 
{ 
    //initializing variables 

    FILE *es; 
    FILE *hs; 
    int num=0; 
    int i; 
    int j; 
    int cols=10; 
    int rows=10; 
    int qid, s; 
    int quizarray[0][0]; 
    int MAX_LENGTH=15; 
    char *result0; 
    char line[MAX_LENGTH]; 
    FILE *qs="C:\\quiz_scores.txt"; 

    qs = fopen("quiz_scores.txt", "r"); 

    /*for (i=0; i<cols; i++){ 
     for (j=0; j<rows; j++){ 
      quizarray[i][j]=0; 
      fscanf(qs, "%d%d", quizarray[i][j]); 
     } 
    } 
*/ 
    while(fgets(line, MAX_LENGTH, qs)) 
    { 
     printf("%s", line); 
    } 
    if(qs == NULL) 
    { 
     printf("Error: Could not open file\n"); 
     return -1; 
    } 
    /*for (i=0;i<n;i++) 
    { 
     fprintf(qs, "%d\n"); 
    } 
    fclose(qs);*/ 
    return 0; 
} 
+0

Вы должны отформатировать и убрать свой вопрос, если вы ожидаете, что люди помогут вам. – xxbbcc

+0

Я так потерял, что не могу этого сделать. – Vine

+0

Что такое содержимое файла? – BLUEPIXY

ответ

0

Хорошо, я думаю, я знаю, что вы хотите, чтобы это произошло, читать числа из файла quiz_scores.txt в ваш 2d массива и напечатать их, правильно ?. Надеюсь, эти исправления позволят вам сделать именно это, а также понять, почему это не сработало раньше. Остальная часть программы предназначена для вас, удачи!

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

int main(void) 
{ 
    //initializing variables 
    int num=0,i,j,cols=10,rows=10;//just smooshed all onto the same line cause it was annoying me 
    int quizarray[10][10];//You declared a 2d array with 0 elements(you wanted one with ten rows and columns I presume 10 teams 10 rounds) 
    FILE *qs = fopen("quiz_scores.txt", "r");//Your declaration of the file pointer was incorrect 
    if(qs == NULL)//This if needs to go before you read anything from the file otherwise its kind of redundant 
    { 
     printf("Error: Could not open file\n"); 
     return -1; 
    } 

    for (i=0; i<cols; i++) 
    { 
     for (j=0; j<rows; j++) 
     { 
      quizarray[i][j]=0; 
      fscanf(qs,"%d", &quizarray[i][j]);//Them scanfs need &s mate 
      \\and don't try to scan in two integer into one array element 
      printf("%d",quizarray[i][j]); 
      if(j<rows-1) 
       printf("-"); 
     } 
     printf("\n"); 
    } 

    fclose(qs);//Dont comment fclose out bad things will happen :P 
    return 0; 
} 
Смежные вопросы