需要帮助解决fscanf和fprintf问题

时间:2018-11-20 20:59:38

标签: c scanf

我的程序应该从.txt文件中读取4个数字,将10个数字加起来,然后再打印回去。我有一个函数,用四个数字加载文本文件,然后另一个函数加10并追加文件。该程序当前可以正常工作,但是我在addTen()函数中感到非常困惑。

为什么我不需要再次fscanf文件?我的函数如何才能知道.txt文件中保存的值?当试图使我的程序与EOF指示器一起使用时,我偶然发现了这一点。

#include <stdio.h>

// function prototypes
void loadTextFile(FILE *file);
void addTen(FILE *file);

// begin main function
int main(void){

    FILE *text = fopen("question6.txt","w");

    if(text == NULL)
    {
        printf("question6.dat cannot be opened!\n");
        fprintf(stderr, "Error opening the fil!\n");
    }

    else
        printf("\nquestion6.dat was opened successfully for Writing.\n\n");

    loadTextFile(text);
    addTen(text);

    fclose(text);

    return 0;
} // end main function


void addTen(FILE *file){

    int i=0;
    int number[4];

    for (i=0;i<4;i++)
    {   
        fprintf(file, "%d\n", number[i]+10);
        printf("\n\t%d was written to the file\n", number[i]+10);
    }

} 

// function to load the .txt file with array values so we may execute main on any computer. 
void loadTextFile(FILE *file){


    int numberArray[4]={56, 23, 89, 30};

    int i=0;

    for(i=0;i<4;i++)
    {
        fprintf(file, "%d\n", numberArray[i]);
        printf("\n\t%d was written to the file\n", numberArray[i]);

    }

    printf("\nThe data was successfully written\n");

} // end function loadTextFile

3 个答案:

答案 0 :(得分:2)

  

该程序当前有效

不,不是!

您的addTen函数具有未定义的行为。它从未初始化的数组输出值。如果它为您“工作”,那可能是因为它恰好与您在丢弃之前在numberArray中填充的局部变量loadTextFile处于堆栈的同一部分。您完全不能依赖此

我建议您将数组传递给函数:

void addTen(int array[], int size) {
    for (int i=0; i < size; i++) {   
        array[i] += 10;
    }
}

void loadTextFile(FILE *file, int array[], int size) {
    for (int i=0; i < size; i++) {   
        fscanf(file, "%d", &array[i]);
    }
}

也许还有一个单独的函数来打印数据:

void printArray(FILE* fp, int array[], int size) {
    for (int i=0; i < size; i++) {   
        fprintf("%d\n", array[i]);
    }
}

现在将其全部调用:

int array[4];
loadTextFile(text, array, 4);
addTen(array, 4);
printArray(text, array, 4);

请注意,您的程序框架中还有其他错误。一个问题是您正在使用openmode "w"打开输入文件,它将以只读方式打开文件,并截断其当前内容。

最好先将其打开以进行读取("r"),然后加载数据并关闭它。然后打开它进行写入并输出修改后的数据。

答案 1 :(得分:0)

结果证明我确实需要fscanf,而我工作的唯一原因是因为我很幸运!在@paddy和其他评论者的帮助下,我得以解决该程序。我还使用预加载具有特定编号的文本文件的想法来解决了该程序。下面的代码是问题中发布的代码的改进版本,可以用作该问题的解决方案。

//Program which reads four numbers from a text document, adds 10 to them and appends them back to the text file

#include <stdio.h>

// function prototypes
void loadTextFile(FILE *file, int array[], int size);
void printArray(FILE* fp, int array[], int size);
void addTen(int array[], int size);

// begin main function
int main(void){

    FILE *text = fopen("question6.txt","r"); // open the file to read it

    int array[4]; 

    loadTextFile(text, array, 4); // scan the .txt file into an array
    addTen(array, 4);   // add ten to each number in the array

    fclose(text); // close the file for writing

    fopen("question6.txt","a"); // open the file to append it

    printArray(text, array, 4); // print the array to the file

    fclose(text); // close the file after appending. 

    return 0;
} // end main function


void loadTextFile(FILE *file, int array[], int size) {
    int i=0;
    for (i=0; i < size; i++) {   
        fscanf(file, "%d", &array[i]);
    }
}

void addTen(int array[], int size) {
    int i=0;
    for (i=0; i < size; i++) {   
        array[i] += 10;
    }
}

void printArray(FILE* fp, int array[], int size) {
    int i=0;
    for (i=0; i < size; i++) {   
        fprintf(fp, "%d\n", array[i]);
        printf( "%d was written to the array.\n", array[i]);
    }
}

通过将指针指向文件的开头,我也有了最初的想法。通过仅使用fopen打开一次文件,指针始终位于文件的末尾。

#include <stdio.h>

// function prototypes

void loadTextFile(FILE *file);

void returnStats(FILE *file);

// begin main function

int main(void){

FILE *text = fopen("question6.txt","w");

if(text == NULL)

{

printf("question6.dat cannot be opened!\n");

fprintf(stderr, "Error opening the fil!\n");

}

else

printf("\nquestion6.dat was opened successfully for Writing.\n\n");

loadTextFile(text);
fclose(text);
FILE *text1 = fopen("question6.txt","r");

returnStats(text1);


return 0;

} // end main function

void returnStats(FILE *file){

int i=0, count;

int number[4];


while(fscanf(file, "%d", &number[i++])!=EOF){

printf("\n%d : Was Read from the File\n", number[i-1]);
count++;

}
fclose(file);
FILE *text1 = fopen("question6.txt","a");
for(i=0;i<count-1;i++){
    fprintf(text1, "%d\n", number[i]+10);
    printf("\n%d: was appended to the text file\n", number[i]+10);
}
} // end returnStats

// function to load the .txt file with array values so we may execute main on any computer.

void loadTextFile(FILE *file){

int numberArray[4]={56, 23, 89, 30};

int i=0;

for(i=0;i<4;i++)

{

fprintf(file, "%d\n", numberArray[i]);

printf("\n%d : was written to the file\n", numberArray[i]);

}

printf("\nThe data was successfully written\n");

} // end function loadTextFile

答案 2 :(得分:-1)

您的loadTextFile(FILE * file)有一个包含4个整数的数组。它不是从文件中读取数字,而是在文件中写入数字。它具有以下数组:int numberArray [4] = {56,23,89,30};

要读取文件,您需要使用fscanf()

相关问题