我正在讀取文本文件並嘗試在控制檯上顯示其內容。這是我的代碼:讀取C中的文本文件
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <fstream>
int main()
{
FILE* fp=NULL;
char buff[100];
fp=fopen("myfile.txt","r");
if(fp==NULL)
{
printf("Couldn't Open the File!!!\n");
}
fseek(fp, 0, SEEK_END);
size_t file_size = ftell(fp);
fread(buff,file_size,1,fp);
printf("Data Read [%s]",buff);
fclose(fp);
return 0;
}
但是隻有冗餘數據顯示在控制檯上;有人能指出我的錯誤嗎?
呀,格式化...... – 2013-04-05 06:27:03
嘗試一個事情......做到這一點.. char * buff ...然後printf(「Data Read%s」,buff); ..讓我知道它是否工作......我已經解決了我的問題,不完全是多一點點... – 2013-04-05 06:28:56
http://stackoverflow.com/questions/410943/reading-a-text-file-into-an-array-in-c – 2013-04-05 06:29:45