问题描述
我似乎找不到 fopen 问题的答案。这似乎很常见,但我尝试的一切都不起作用,从我读过的内容来看,这似乎是由尾随换行符引起的,但我不确定:
任何帮助将不胜感激
我的代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main( int argc,char *argv[] )
{
setbuf(stdout,NULL);
struct Person { char lname[20]; char fname[20]; int id; };
int N1,i ;
struct Person *p;
// char filename[] = "../src/students.csv" ;
//
// FILE *str = fopen( filename,"r") ;
// if (str==NULL) {
// perror(filename) ;
// exit(-1) ;
// }
printf("Please input file name:"); // Asks user to enter file name.
FILE *str2 = fopen(argv[1]," r"); // File opened for reading
printf("%p",str2);
if (str2 == NULL) { // If issue opening file display the error.
perror(argv[1]);
exit(-1);
}
fscanf( str2,"%d\n",&N1 ) ;
p = (struct Person*)calloc( N1,sizeof(struct Person) ) ;
if (p==NULL) {
fprintf(stderr,"Can't allocate %d structs -- exiting.\n",N1 ) ;
exit(-1) ;
}
for ( i=0 ; i<N1 ; i++ ) {
fscanf( str2," %[^,],%[^,B%d",p[i].lname,p[i].fname,&(p[i].id) );
}
fclose(str2) ;
/* Access the array... Just like a static array */
printf("%s,%s,B000%d\n",p[4].lname,p[4].fname,p[4].id) ;
free(p) ;
return 0;
}
studnets.csv
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)