C 为什么我的printf()没有打印文本文件中的第一个字符?
我是一个noobc程序员。 我无法读取文本文件中的所有字符。 我的file.txt有数字:9,2,3,4,5,6,但当我运行下面编写的代码时,它只跳过9并打印其余的数字。然而,当我在9之前加一个空格时,它运行得非常好。我怎么修理它?为什么会这样C 为什么我的printf()没有打印文本文件中的第一个字符?,c,file,error-handling,file-handling,C,File,Error Handling,File Handling,我是一个noobc程序员。 我无法读取文本文件中的所有字符。 我的file.txt有数字:9,2,3,4,5,6,但当我运行下面编写的代码时,它只跳过9并打印其余的数字。然而,当我在9之前加一个空格时,它运行得非常好。我怎么修理它?为什么会这样 FILE* fp; fp = fopen("file.txt", "r"); int a[10]; char ch; while((ch=getc(fp))!= EOF) { fscanf(fp, "%d", &a[ch]);
FILE* fp;
fp = fopen("file.txt", "r");
int a[10];
char ch;
while((ch=getc(fp))!= EOF)
{
fscanf(fp, "%d", &a[ch]);
printf("%d ", a[ch]);
}
提前感谢 函数
getc
在中,而((ch=getc(fp))!=EOF)
使用文件中的单个字符;如果9
是文件中的第一个字符,它将被读入ch
,并且-由于您的字符集可能是ASCII-将设置ch==0x39
因此(1)9
将不再适用于fscanf(fp、%d、&a[ch])代码>;这就是为什么你认为它是“跳过的”
(2) 在ch
位置写入a
,然后会超出a
的数组边界。不清楚文件中有多少int
,当前存储它们的方式很容易出错fgetc
返回一个int
值,该值为EOF
(-1)或0-255,因此ch
应声明为int
,然后当(ch=getc(fp))=EOF
为真,ch
将包含0-255。
在下面的fscanf(fp、%d、&a[ch])中
您再次从文件中读取,但在索引位置ch
处读取到数组中,这显然不是您的意图(当您读取值>9的字符时,它也可能会崩溃。提示:“0”的值为48)。下面的解决方案看起来过于复杂,但我已经习惯C++了,这是在引擎盖下发生的,你不必为此费心了。也许你会从中得到一些想法
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
/* dynamic memory storage - start */
typedef struct {
size_t m_size;
size_t m_reserved;
int* m_data;
} store;
/* create a store */
store* store_create() {
store* s = malloc(sizeof(store));
if(s) {
/* initialize values */
s->m_size = 0;
s->m_reserved = 1; /* a very conservative start value */
s->m_data = malloc(sizeof(int)*s->m_reserved);
if(s->m_data==NULL) {
free(s);
s = NULL;
}
}
return s;
}
/* destroy a store */
void store_destroy(store* s) {
/* free the integer array */
free(s->m_data);
/* free the store struct */
free(s);
}
bool store_reserve(store* s, size_t new_res) {
/* reserve more space for the int's if needed */
if(new_res>s->m_reserved) {
int* n = realloc(s->m_data, sizeof(int)*new_res);
if(n==NULL) return false; /* could not expand storage */
s->m_reserved = new_res;
s->m_data = n;
}
return true;
}
// check if it's time to increase reserved storage
bool store_check_reserve(store* s) {
if(s->m_size == s->m_reserved)
/* change 5/4 to a larger value for more aggressive
* increase of memory allocation */
return store_reserve(s, (s->m_reserved+1)*5/4);
else
return true;
}
// add a value to the store
bool store_add(store* s, int v) {
if(!store_check_reserve(s)) return false;
s->m_data[s->m_size++] = v;
return true;
}
/* dynamic memory storage - end */
int main(int argc, char* argv[]) {
FILE* fp;
fp = fopen("file.txt", "r");
/* create a store for our unknown amount of int's */
store* myStore = store_create();
/* scan for strings separated by comma and newline
* and allocate memory for it */
char* str;
while(fscanf(fp, " %m[^,\n],", &str)==1)
{
int num;
/* convert string to integer */
if(sscanf(str, "%d", &num)==1) {
/* store the extracted value */
if(store_add(myStore, num)==false) {
fprintf(stderr, "FAILED STORING %d\n", num);
}
}
// free string allocated by fscanf (%m) */
free(str);
}
fclose(fp);
printf("All %d stored values:\n", myStore->m_size);
for(size_t i=0; i<myStore->m_size; ++i) {
printf("%d = %d\n", i, myStore->m_data[i]);
}
/* release memory allocated by our store */
store_destroy(myStore);
return 0;
}
#包括
#包括
#包括
/*动态内存存储-启动*/
类型定义结构{
大小;
保留大小;
int*m_数据;
}储存;
/*创建一个商店*/
store*store_create(){
store*s=malloc(sizeof(store));
若有(s){
/*初始化值*/
s->m_尺寸=0;
s->m_reserved=1;/*一个非常保守的起始值*/
s->m_数据=malloc(sizeof(int)*s->m_保留);
如果(s->m_数据==NULL){
免费的;
s=零;
}
}
返回s;
}
/*摧毁商店*/
作废存储\销毁(存储*s){
/*释放整数数组*/
免费(s->m_数据);
/*释放存储结构*/
免费的;
}
布尔商店保留区(商店数量、大小和新商店){
/*如果需要,为int保留更多空间*/
如果(新建资源>保留资源->保留资源){
int*n=realloc(s->m_data,sizeof(int)*new_res);
如果(n==NULL)返回false;/*无法扩展存储*/
s->m_reserved=新的_res;
s->m_数据=n;
}
返回true;
}
//检查是否是增加保留存储的时候了
布尔商店、检查、保留区(商店*s){
如果(s->m_大小==s->m_保留)
/*将5/4更改为更大的值以提高攻击性
*增加内存分配*/
退货存储保留(s->m\U保留+1)*5/4);
其他的
返回true;
}
//为存储添加一个值
bool store_add(store*s,int v){
如果(!store_check_reserve)返回false;
s->m_数据[s->m_大小+]=v;
返回true;
}
/*动态内存存储-结束*/
int main(int argc,char*argv[]){
文件*fp;
fp=fopen(“file.txt”,“r”);
/*为未知数量的int创建一个存储*/
store*myStore=store_create();
/*扫描以逗号和换行符分隔的字符串
*并为其分配内存*/
char*str;
而(fscanf(fp,“%m[^,\n],”,&str)==1)
{
int-num;
/*将字符串转换为整数*/
如果(sscanf(str,“%d”,&num)==1){
/*存储提取的值*/
if(store_add(myStore,num)==false){
fprintf(stderr,“存储%d\n失败”,num);
}
}
//fscanf分配的可用字符串(%m)*/
自由基(str);
}
fclose(fp);
printf(“所有%d个存储值:\n”,myStore->m_size);
对于(大小i=0;im大小;++i){
printf(“%d=%d\n”,i,myStore->m_data[i]);
}
/*释放我们商店分配的内存*/
商店销毁(myStore);
返回0;
}
a[10]
太小,无法用ascii字符索引!你把它放在你的记忆之外:未定义的行为。另外,getc
获取一个字符,在你的例子中,第一个9
,然后用fscanf
读取2
,然后用getc
读取3
,然后……等等……我不确定你想做什么。这一切看起来都很笨拙。请注意,getc()
返回一个int
值,char
可能无法保持EOF
,因此char ch代码>-->intch
。那么,我应该怎么做才能有效地读取或扫描文件中的所有整数?@Xedron您能解释一下为什么要使用ch
作为a[]数组的索引吗?您输入的文件是什么样子的。这很难提供帮助,因为您的代码不仅仅是从文件中读取整数。