C 这是打开.txt文件的正确方法吗
这部分代码负责打开我的data.txt文件并从中读取几个数字(数组的大小和要填充的数字),但Virtual Programming Lab和C都表示“无法在可执行文件中找到程序”。我试着用这种方法写了一个程序,它只能从.txt中读取1个数字,同样的错误也会出现,所以我想我完全错了。是否有其他方法读取.txt文件?谢谢C 这是打开.txt文件的正确方法吗,c,C,这部分代码负责打开我的data.txt文件并从中读取几个数字(数组的大小和要填充的数字),但Virtual Programming Lab和C都表示“无法在可执行文件中找到程序”。我试着用这种方法写了一个程序,它只能从.txt中读取1个数字,同样的错误也会出现,所以我想我完全错了。是否有其他方法读取.txt文件?谢谢 Array* input() { int x,y; int i; Array *p; FILE *f=fopen("data.txt", "r"); p = (Array*)
Array* input() {
int x,y;
int i;
Array *p;
FILE *f=fopen("data.txt", "r");
p = (Array*) malloc(sizeof(Array));
fscanf(f, "&d",&x);
fscanf(f, "&d",&y);
p->n1 = x;
p->n2 = y;
p->p1 = (int*) malloc(sizeof(int) * p->n1);
p->p2 = (int*) malloc(sizeof(int) * p->n2);
for(i=0; i < p->n1; i++)
fscanf(f, "%d", &p->p1[i]);
for(i=0; i < p->n2; i++)
fscanf(f, "%d", &p->p2[i]);
fclose(f);
return p;
}
Array*input(){
int x,y;
int i;
数组*p;
文件*f=fopen(“data.txt”,“r”);
p=(数组*)malloc(sizeof(数组));
fscanf(f、&d、&x);
fscanf(f、&d、&y);
p->n1=x;
p->n2=y;
p->p1=(int*)malloc(sizeof(int)*p->n1);
p->p2=(int*)malloc(sizeof(int)*p->n2);
对于(i=0;in1;i++)
fscanf(f,,%d,&p->p1[i]);
对于(i=0;in2;i++)
fscanf(f,,%d,&p->p2[i]);
fclose(f);
返回p;
}
以下建议的代码:
#include <stdio.h>
#include <stdlib.h>
struct array
{
size_t n1;
size_t n2;
int *p1;
int *p2;
};
typedef struct array ARRAY;
ARRAY* input( void )
{
size_t x;
size_t y;
ARRAY *p;
FILE *f = fopen( "data.txt", "r" );
if( !f )
{
perror( "fopen for reading data.txt failed" );
exit( EXIT_FAILURE );
}
p = malloc( sizeof(ARRAY) );
if( !p )
{
perror( "malloc for an instance of struct array failed" );
fclose( f );
exit( EXIT_FAILURE );
}
if( fscanf( f, "%zu", &x ) != 1 )
{
fprintf( stderr, "fscanf for first data from file failed\n" );
fclose( f );
free( p->p1 );
free( p->p2 );
free( p );
exit( EXIT_FAILURE );
}
if( fscanf( f, "%zu", &y ) != 1 )
{
fprintf( stderr, "fscanf for second data from file failed\n" );
fclose( f );
free( p );
exit( EXIT_FAILURE );
}
p->n1 = x;
p->n2 = y;
p->p1 = malloc(sizeof(int) * p->n1);
if( !p->p1 )
{
perror( "malloc failed for pointer 'p1'" );
free( p );
fclose( f );
exit( EXIT_FAILURE );
}
p->p2 = malloc(sizeof(int) * p->n2);
if( !p->p2 )
{
perror( "malloc failed for pointer 'p2'" );
free( p->p1 );
free( p );
fclose( f );
exit( EXIT_FAILURE );
}
for( size_t i=0; i < p->n1; i++ )
{
if( fscanf(f, "%d", &p->p1[i]) != 1 )
{
fprintf( stderr, "fscanf for %zu value in array 'p1' failed\n", i );
fclose(f);
free( p->p1 );
free( p->p2 );
free( p );
exit( EXIT_FAILURE );
}
}
for( size_t i=0; i < p->n2; i++ )
{
if( fscanf(f, "%d", &p->p2[i]) != 1 )
{
fprintf( stderr, "fscanf for %zu value in array 'p2' failed\n", i );
fclose(f);
free( p->p1 );
free( p->p2 );
free( p );
exit( EXIT_FAILURE );
}
}
fclose(f);
return p;
}
int main( void )
{
ARRAY *ptrToArray = input();
free( ptrToArray->p1 );
free( ptrToArray->p2 );
free( ptrToArray );
return 0;
}
malloc()
需要类型为size\t
的参数。这导致需要从int
到size\u t
用于如果否则当执行时…当通过单个空行切换案例
input()
asvoid
i
的范围限制在其声明的代码块内,而不是函数的范围内:input()
#include <stdio.h>
#include <stdlib.h>
struct array
{
size_t n1;
size_t n2;
int *p1;
int *p2;
};
typedef struct array ARRAY;
ARRAY* input( void )
{
size_t x;
size_t y;
ARRAY *p;
FILE *f = fopen( "data.txt", "r" );
if( !f )
{
perror( "fopen for reading data.txt failed" );
exit( EXIT_FAILURE );
}
p = malloc( sizeof(ARRAY) );
if( !p )
{
perror( "malloc for an instance of struct array failed" );
fclose( f );
exit( EXIT_FAILURE );
}
if( fscanf( f, "%zu", &x ) != 1 )
{
fprintf( stderr, "fscanf for first data from file failed\n" );
fclose( f );
free( p->p1 );
free( p->p2 );
free( p );
exit( EXIT_FAILURE );
}
if( fscanf( f, "%zu", &y ) != 1 )
{
fprintf( stderr, "fscanf for second data from file failed\n" );
fclose( f );
free( p );
exit( EXIT_FAILURE );
}
p->n1 = x;
p->n2 = y;
p->p1 = malloc(sizeof(int) * p->n1);
if( !p->p1 )
{
perror( "malloc failed for pointer 'p1'" );
free( p );
fclose( f );
exit( EXIT_FAILURE );
}
p->p2 = malloc(sizeof(int) * p->n2);
if( !p->p2 )
{
perror( "malloc failed for pointer 'p2'" );
free( p->p1 );
free( p );
fclose( f );
exit( EXIT_FAILURE );
}
for( size_t i=0; i < p->n1; i++ )
{
if( fscanf(f, "%d", &p->p1[i]) != 1 )
{
fprintf( stderr, "fscanf for %zu value in array 'p1' failed\n", i );
fclose(f);
free( p->p1 );
free( p->p2 );
free( p );
exit( EXIT_FAILURE );
}
}
for( size_t i=0; i < p->n2; i++ )
{
if( fscanf(f, "%d", &p->p2[i]) != 1 )
{
fprintf( stderr, "fscanf for %zu value in array 'p2' failed\n", i );
fclose(f);
free( p->p1 );
free( p->p2 );
free( p );
exit( EXIT_FAILURE );
}
}
fclose(f);
return p;
}
int main( void )
{
ARRAY *ptrToArray = input();
free( ptrToArray->p1 );
free( ptrToArray->p2 );
free( ptrToArray );
return 0;
}
#包括
#包括
结构体数组
{
大小为n1;
尺寸为n2;
int*p1;
int*p2;
};
typedef结构数组;
数组*输入(无效)
{
尺寸x;
大小;
数组*p;
文件*f=fopen(“data.txt”,“r”);
如果(!f)
{
perror(“读取数据的fopen.txt失败”);
退出(退出失败);
}
p=malloc(sizeof(数组));
如果(!p)
{
perror(“结构数组实例的malloc失败”);
fclose(f);
退出(退出失败);
}
如果(fscanf(f,%zu,&x)!=1)
{
fprintf(stderr,“来自文件的第一个数据的fscanf失败\n”);
fclose(f);
自由(p->p1);
自由(p->p2);
自由基(p);
退出(退出失败);
}
如果(fscanf(f,%zu,&y)!=1)
{
fprintf(stderr,“文件中第二个数据的fscanf失败\n”);
fclose(f);
自由基(p);
退出(退出失败);
}
p->n1=x;
p->n2=y;
p->p1=malloc(sizeof(int)*p->n1);
如果(!p->p1)
{
perror(“指针'p1'的malloc失败”);
自由基(p);
fclose(f);
退出(退出失败);
}
p->p2=malloc(sizeof(int)*p->n2);
如果(!p->p2)
{
perror(“指针“p2”的malloc失败);
自由(p->p1);
自由基(p);
fclose(f);
退出(退出失败);
}
对于(大小i=0;in1;i++)
{
如果(fscanf(f,“%d”,&p->p1[i])!=1)
{
fprintf(stderr,“数组'p1'中%zu值的fscanf失败\n”,i);
fclose(f);
自由(p->p1);
自由(p->p2);
自由基(p);
退出(退出失败);
}
}
对于(大小i=0;in2;i++)
{
如果(fscanf(f,“%d”,&p->p2[i])!=1)
{
fprintf(stderr,“数组'p2'中%zu值的fscanf失败\n”,i);
fclose(f);
自由(p->p1);
自由(p->p2);
自由基(p);
退出(退出失败);
}
}
fclose(f);
返回p;
}
内部主(空)
{
数组*ptrToArray=input();
空闲(ptrToArray->p1);
空闲(ptrToArray->p2);
自由(ptrToArray);
返回0;
}
来自生成系统的错误消息看起来与源代码中的任何问题无关。您是否在开发环境中设置了输出程序的名称?除了完全没有错误检查之外,代码本身看起来或多或少还行,在第二个for
循环中,您使用了p1
而不是p2
。感谢Ian,在第二个循环中错过了这个错误。那一定是输出问题,因为现在我尝试运行程序时内存不足,这一定是因为我没有正确打印数组。请提供一个示例,并向我们展示一些输入示例。当询问有关运行时问题的问题时,正如这个问题所做的那样,发布一篇文章,这样我们可以重现问题并帮助您调试它。OT:为了便于阅读和理解:1)一致地缩进代码。在每个左大括号“{”之后缩进。在每个右大括号“}”之前取消缩进。建议每个缩进级别为4个空格。