我很难从另一个c程序编写的文件中获取变量/元素的值,以存储到正在运行的c程序的变量/元素中。 将变量“ i”读入程序(这是一个已经确定并存储在文件“ inventory.txt”中的整数)时,我总是遇到分段错误。 “ i”表示要打印的数组的元素数量(大小)。这是损坏的代码):。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int l, k;
int *pn, *arraysize, *q;
float *p;
FILE *fp = fopen( "inventory.txt", "r+b" ); //declarations and I open up the binary file "inventory.txt"
if ( fp == NULL )
{ //check file
printf( "nah" );
exit( EXIT_FAILURE );
}
*arraysize = fread( &arraysize, sizeof( int ), 1, fp ); //set arraysize in this program equal to i in the binary file inventory.txt?????? Here is where I get segmentation fault
printf( "marker1" ); //place marker, I am not getting to this point when running the program
for ( l = 0; l < *arraysize; l++ )
{
q[l] = fread( &q[l], sizeof( int ), arraysize, fp ); //set array elements ("arraysize" of them) pn, q, and p to the array elements of the same name in binary file inventory.txt
p[l] = fread( &p[l], sizeof( int ), arraysize, fp );
pn[l] = fread( &pn[l], sizeof( int ), arraysize, fp );
}
printf( "Below are the items in your inventory.\nPart#\tQuantity\tPrice\n" ); //print out the values
for ( l = 0; l < *arraysize; l++ )
{
printf( "%5d\t", pn[l] );
printf( "%8d\t", q[l] );
printf( "%9.2f\n", p[l] );
}
return 0;
}
答案 0 :(得分:0)
这是您的一些代码:
int *arraysize;
*arraysize=fread(&arraysize,sizeof(int),1,fp);
您将arraysize
声明为int *
(一个指针),但是随后您尝试将一个int读入其中。这是没有道理的。然后,您尝试取消对它的引用以存储返回值,该返回值将崩溃,因为它不是有效的指针。
您可能想要
int arraysize;
if (fread(&arraysize, sizeof(int), 1, fp) != 1) {
fprintf(stderr, "error reading file\n");
exit(1); }
将arraysize
声明为一个简单的int(不是指针)并读入其中。
您的其他fread呼叫也有类似的问题。
答案 1 :(得分:0)
您的指针有点错误,该代码从未分配任何内存来读取值,并且fread()
返回读取的项数,该代码(由于某种原因)覆盖了以此方式读取的值结果。
这里是补丁版本,但是您不包含任何测试数据,因此未经测试。
#include <stdio.h>
#include <stdlib.h>
int main( )
{
int l;
int *pn;
int *q;
float *p;
int arraysize;
FILE *fp = fopen( "inventory.txt", "r+b" ); //declarations and I open up the binary file "inventory.txt"
if ( fp == NULL )
{ //check file
printf( "nah" );
exit( EXIT_FAILURE );
}
fread( &arraysize, sizeof( int ), 1, fp ); //set arraysize in this program equal to i in the binary file inventory.txt?????? Here is where I get segmentation fault
printf( "marker1" ); //place marker, I am not getting to this point when running the program
// Allocate some memory to hold the data (Yes! With cast on malloc())
q = (int *)malloc(arraysize * sizeof( int )); // TODO - check malloc() OK
pn = (int *)malloc(arraysize * sizeof( int )); // TODO - check malloc() OK
p = (float *)malloc(arraysize * sizeof( float )); // TODO - check malloc() OK
// read each entire block in a single operation
fread( q, sizeof( int ), arraysize, fp ); //set array elements ("arraysize" of them) pn, q, and p to the array elements of the same name in binary file inventory.txt
fread( p, sizeof( float ), arraysize, fp );
fread( pn, sizeof( int ), arraysize, fp );
printf( "Below are the items in your inventory.\nPart#\tQuantity\tPrice\n" ); //print out the values
for ( l = 0; l < arraysize; l++ )
{
printf( "%5d\t", pn[l] );
printf( "%8d\t", q[l] );
printf( "%9.2f\n", p[l] );
}
return 0;
}