2013-04-05 97 views
0

我正在讀取文本文件並嘗試在控制檯上顯示其內容。這是我的代碼:讀取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; 
} 

但是隻有冗餘數據顯示在控制檯上;有人能指出我的錯誤嗎?

+5

呀,格式化...... – 2013-04-05 06:27:03

+0

嘗試一個事情......做到這一點.. char * buff ...然後printf(「Data Read%s」,buff); ..讓我知道它是否工作......我已經解決了我的問題,不完全是多一點點... – 2013-04-05 06:28:56

+0

http://stackoverflow.com/questions/410943/reading-a-text-file-into-an-array-in-c – 2013-04-05 06:29:45

回答

3

你需要閱讀之前尋求迴文件的開頭:

int main() 
{ 
    FILE* fp=NULL; 
    char buff[100]; 
    fp=fopen("myfile.txt","r"); 
    if(fp==NULL) 
    { 
     printf("Couldn't Open the File!!!\n"); 
     exit(1);      // <<< handle fopen failure 
    } 
    fseek(fp, 0, SEEK_END); 
    size_t file_size = ftell(fp); 
    fseek(fp, 0, SEEK_SET);   // <<< seek to start of file 
    fread(buff,file_size,1,fp); 
    printf("Data Read [%s]",buff); 
    fclose(fp); 
    return 0; 
} 
+0

非常感謝:)@ Paul R – 2013-04-05 06:43:36

4

你忘了復位文件指針這樣做之後啓動。

fseek(fp, 0, SEEK_END); 

找到大小(file_size)後,請執行此操作。

rewind (fp); 
+0

或者:'fseek(fp,0L,SEEK_SET);' – 2013-04-05 06:32:16

+0

@JonathanLeffler是的。這也將起作用。也許倒帶(fp)調用fseek來完成這項工作。 – dejavu 2013-04-05 06:35:28

+1

非常感謝Android Decoded和Jonathan Leffler :) :) – 2013-04-05 06:42:46

0

嘗試....

#include <stdio.h> 
#include <stdlib.h> 

void handle_line(char *line) { 
printf("%s", line); 
} 

int main(int argc, char *argv[]) { 
int size = 1024, pos; 
int c; 
char *buffer = (char *)malloc(size); 

FILE *f = fopen("myfile.txt", "r"); 
if(f) { 
    do { // read all lines in file 
    pos = 0; 
    do{ // read one line 
     c = fgetc(f); 
     if(c != EOF) buffer[pos++] = (char)c; 
     if(pos >= size - 1) { // increase buffer length - leave room for 0 
     size *=2; 
     buffer = (char*)realloc(buffer, size); 
     } 
    }while(c != EOF && c != '\n'); 
    buffer[pos] = 0; 
    // line is now in buffer 
    handle_line(buffer); 
    } while(c != EOF); 
    fclose(f);   
} 
free(buffer); 
return 0; 

}

+0

這是很多代碼。你檢查'fopen()'的結果,如果打開失敗,不要使用它 - 這很好。理論上,你應該檢查'malloc()'。你不應該使用'buffer = realloc(buffer,size);'因爲如果'realloc()'失敗,你就失去了唯一的指向分配內存的指針,因此泄漏了之前分配的內容。我想我會使用'fgets()'或POSIX'getline()'而不是逐個字符的輸入。 – 2013-04-05 06:36:32

0
#include "stdafx.h" 
    #include <stdio.h> 
    #include <string.h> 
    #include <fstream> 

    int main() 
    { 
     FILE* fp=NULL; 
     char *buff;      //change array to pointer 
     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); 
     buff = malloc(file_size);  //allocating memory needed for reading file data 
     fseek(fp,0,SEEK_SET);   //changing fp to point start of file data 
     fread(buff,file_size,1,fp); 
     printf("Data Read [%s]",buff); 
     fclose(fp); 
     return 0; 
    } 
0

具有100個字節的緩衝器讀取文件是不是一個好主意作爲由於文件的大小可以是超過100個字節。

更好的文件io可以通過在文件上執行fgets來完成,如果它不是您想要使用fread讀取的元數據類型。

fgets在while循環中可用於檢查其到達的EOF或feof調用是否可用於檢查EOF。

與fgets的樣本代碼清單可以是這樣的:

while (fgets(buf, len, fp)) { 
     printf("%s", buf); 
} 

或者是用於fgets使用,可就是這樣一個樣本:

while (fread(buf, len, 1, fp) >= 0) { 
     printf("%s\n", buf); 
}