java无效的参数_创建文件时“无效参数”

你能帮我创建一个文本文件,因为现在指向文件的* fp指针会向函数fopen返回NULL吗?

使用库 errno.h 和 extern int errno 我得到"Value of errno: 22" .

if (!fp)perror("fopen") 给了我"Error opening file: Invalid argument" .

在我的main函数中,我输入文件的名称:

void main()

{

float **x;

int i,j;

int l,c;

char name_file[30];

FILE *res;

/* some lines omitted */

printf("\nEnter the name of the file =>");

fflush (stdin);

fgets(name_file,30,stdin);

printf("Name of file : %s", name_file);

res=creation(name_file,l,c,x);

printf("\nThe created file\n");

readfile(res,name_file);

}

创建文本文件的功能:

FILE *creation(char *f_name,int l, int c, float **a) // l rows - c colums - a array

{ FILE *fp;

int i,j;

fp = fopen(f_name,"wt+"); // create for writing and reading

fflush(stdin);

/* The pointer to the file is NULL: */

if (!fp)perror("fopen"); // it's returning Invalid argument

printf("%d\n",fp); //0 ??

if(fp==NULL) { printf("File could not be created!\n"); exit(1); }

fflush(stdin);

for(i=0;i

{

for(j=0;j

{

fprintf(fp,"%3.2f",a[i][j]); // enter every score of the array in the text file

}

fprintf(fp,"\n");

}

return fp;

}

读取文件并检查文件是否正确的功能:

**void readfile(FILE *fp,char *f_name)**

{

float a;

rewind(fp);

if(fp==NULL) { printf("File %s could not open\n",f_name); exit(1); }

while(fscanf(fp,"%3.2f",&a)!= EOF)

printf("\n%3.2f",a);

fclose(fp);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值