【发布时间】:2015-02-25 12:38:27
【问题描述】:
我正在尝试使用 ubuntu 在 eclipse 上运行我的代码。
我已使用 fprintf 将数据转储到一个 txt 文件中,并使用 fscanf 读取该文件。我无法将该值读入数据数组。
下面是我的代码:
#include <stdio.h> /* printf, scanf, NULL */
#include <stdlib.h> /* malloc, free, rand */
int main(){
char* data;
FILE *fp;
size_t result;
data = (char*) malloc (sizeof(char)*(1280*800));//Size of one frame
if (data==NULL){
printf("NOt able to allocate memory properly\n");
exit (1);
}
fp = fopen ("\\home\\studinstru\\Desktop\\filedump.txt", "r");
if(fp==NULL){
printf("Error in creating dump file\n");
exit (1);
}
for(int m = 0;m<1280;m++){
for(int n = 0;n<800;n++){
fscanf(fp,"%d/t",data[m*800 + n]);
}
}
fclose(fp);
return 0;
}
这是我的 filedump.txt 数据:
79 78 78 77 78 79 81 95
82 81 81 81 82 82 82 82
79 78 78 77 78 79 81 95
82 81 81 81 82 82 82 82
79 78 78 77 78 79 81 95
82 81 81 81 82 82 82 82 ....
你能说出这有什么问题吗?
【问题讨论】:
-
尝试刷新和关闭文件。
-
如果这是 C,那么你不应该转换
malloc的结果。如果这是 C++,您应该使用std::string和(通常)C++ I/O。换句话说,C != C++,你通常应该只标记你正在编写/编译的语言。 -
flush和close是什么意思?
-
只读文件上
fprintf(fp,"\n");是什么意思? -
“创建转储文件时出错”是(大部分)无用的错误消息。尝试:
fp=open(path,mode); if(fp==NULL) {perror(path); exit(1);}(并使用 EXIT_FAILURE 而不是 '1')
标签: c file-handling