I'm trying to write a program where I first create a file called test.txt(entered by user),then read from the file and store it into array2[], but got a Access violation reading location error(this is transalated so im not sure if it is the actual error name)
0xC0000005: 寫入位置 0x00000014 時發生存取違規。
0xC0000005: assigning location 0x00000014 occurred access violation。
I am assuming the problem comes from the while part
#include <stdio.h>
FILE *file;
double array[10] = { 0 };
int array2[10] = {0};
char string[20];
int main(void)
{
printf("enter the file you want to open:\n");
scanf("%s",string);
file = fopen(string, "w");
for (int a = 0; a < 10; a++) {
fprintf(file, "%d ", array[a]);
}
fclose(file);
file = fopen(string, "r");
while (file = fscanf(string, "%d %d %d %d %d %d %d %d %d %d ", &array2[0], &array2[1], &array2[2], &array2[3], &array2[4], &array2[5], &array2[6], &array2[7], &array2[8], &array2[9]) != EOF) {
printf("%d %d %d %d %d %d %d %d %d %d ", array2[0], array2[1], array2[2], array2[3], array2[4], array2[5], array2[6], array2[7], array2[8], array2[9]);
}
system("pause");
fclose(file);
}
This
file = fscanf( )
is wrong. check the return value of fscanf()
it doesn't return FILE*
type, it returns int
. From the manual page of fscanf()
int fscanf(FILE *stream, const char *format, ...); RETURN VALUE These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure. The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs.
Also you writing double
elements
double array[10] = { 0 };
into file but while reading storing into int
array like
int array2[10] = {0};
It doesn't result in desired output. declare array
also as int
type.
Also you don't have to use number of format specifier in fscanf()
as you did because you know how many int
elements you wrote in file, instead rotate loop count
times. for e.g
int var = 0,ret = 0;
while( (ret = fscanf(file,"%d",&array2[var])) == 1){
printf("%d\n",array2[var]);
var++;
}
Sample modified code
#include <stdio.h>
/* avoid using global array if not needed, declare locally @TODO */
FILE *file;
int array[10] = {1,2,3,4,5};/* make it as int type, not double */
int array2[10] = {0};
char string[20];
int main(void) {
printf("enter the file you want to open:\n");
scanf("%s",string);
file = fopen(string, "w"); /* check the return value @TODO */
if(file == NULL) {
/* error handling @TODO */
}
int ele = sizeof(array)/sizeof(array[0]);
for (int a = 0; a < ele; a++) {
fprintf(file, "%d ", array[a]); /*not that array contains all 0,so yoiu are writing zero's into file */
}
rewind(file);
fclose(file);
file = fopen(string, "r");
if(file == NULL) {
/* error handling */
}
int var = 0,ret = 0;
while( (ret = fscanf(file,"%d",&array2[var])) == 1){
printf("%d\n",array2[var]);
var++;
}
system("pause");
fclose(file);
}
User contributions licensed under CC BY-SA 3.0