我用的是fscanf
和fprintf
。
我试图用\t
来分隔每一行上的字符串,并这样读:
fscanf(fp,"%d\t%s\t%s",&t->num,&t->string1,&t->string2);
文件内容:
1[TAB]string1[TAB]some string[NEWLINE]
它读得不好如果我得到:
1 string1 some
另外,我得到了这个编译警告:
warning: format specifies type 'char *' but the argument has type 'char (*)[15]' [-Wformat]
如何在不使用二进制r/w的情况下修复此问题?
最佳答案
我猜"some string"
中的空格是问题所在。fscanf()
使用%s
在第一个空白字符处停止读取字符串要包含空格,请使用以下内容:
fscanf(fp, "%d\t%[^\n\t]\t%[^\n\t]", &t->num, &t->string1, &t->string2);
另请参见a reference page for
fscanf()
和/或another StackOverflow thread on reading tab-delimited items in C。[根据您的编辑进行编辑:您传递到
fscanf()
的参数似乎也有问题。您需要张贴t->string1
的声明才能确定,但它看起来像是一个字符数组,因此您应该从string1
调用中删除&