2013-07-02 70 views
3

我的問題是:如何從C中的文件讀取多行中作爲一行字符串的字符串?我試圖做一個二進制映射,我的1D數組表示爲2d數組。因此,這是在我的「level_1.txt」:如何從文件中讀取字符串並將其標記爲數組?

//start of file 
WIDTH: 4 
HEIGHT: 5 

11,12,13,14, 
21,22,23,24, 
31,32,33,34, 
41,42,43,44, 
51,52,53,54, 
// eof 

,我想獲得字符串「11,12,13,14,21,22 ......」

這是我的代碼:

int ImportMapDataFromFile(char *fileName, Map *self) 
{ 
    FILE *pFile; 
    char* myStr; 

    pFile = fopen(fileName, "r"); 

    // Check if the file exists: 
    if(pFile) 
    { 
     // scanf width and height 
     //fscanf(pFile, "%*s %i %*s %i", &self->width, &self->height); 
     /* 
     // this doesnt work 
     fscanf(pFile, "%*s %i %*s %i %s", &self->width, &self->height, &myStr); 
     */ 
     //printf("%i %i", self->width, self->height); 

     // initialise the array 
     (self->theScreenMap) = (Grid*)malloc(sizeof(Grid) * self->width * self->height); 

     // scan the whole remaining file 
     /* 
      I dont know how to do this. I tried using fscanf and had a look at fgets 
      but i cant seem to make it work sorry. 
     */ 


     // tokenise it 
     /* 
      Pretty sure i have to use strtok right? 
      http://www.cplusplus.com/reference/cstring/strtok/ 
     */ 

     // close it 
     fclose(pFile); 
     printf("%s \n", &myStr); 

     return TRUE; 
    } 
    else 
    { 
     fclose(pFile); 
     return FALSE; 
    } 
} 

我想要做的是閱讀文件,得到從第1 2行的規模和使用這些值來創建一維數組。然後,一旦完成,我想讀取剩餘的字符串並將其分配給數組。例如。

theScreenMap[0] = 11; // first element has 1st token 
theScreenMap[1] = 12; 
theScreenMap[size - 1] = 54; // last element has last token 

感謝任何幫助我的人。但是如果任何人有更好的方式來做到這一點(從文件讀取並初始化一個數組來創建二進制映射),那麼請隨時告訴我。謝謝! :)

回答

4

這只是你想要的基本實現。最終結果將讀取與「高度」一樣多的行,並將它們連接起來。

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#define MAXSIZE 1024 
#define MAXLINE 256 

char mainArray[MAXSIZE]; 
char line[MAXLINE]; 

char *strdup(const char *s) { 
    char *str = malloc(strlen(s) + 1); 
    if(str) { strcpy(str, s); } 
    return str; 
} 

int main(int argc,char* argv[]) 
{ 
    int width,height; 
    unsigned int i=0; 
    unsigned int count=0; 
    char **grid; 
    FILE *fp = fopen("multiline.txt","r"); 
    if(!fp) {perror("multiline.txt");return -1;} 
    /* Read the width and height */ 
    fscanf(fp,"WIDTH: %d\n",&width); 
    fscanf(fp,"HEIGHT: %d\n",&height); 
    printf("Width: %d,\tHeight: %d\n\n",width,height); 
    while(!feof(fp)) 
    { 
     fgets(line,MAXLINE,fp); 
     strncat(mainArray,line,strlen(line)-1); /* get rid of newline */ 
     printf("Line:: %s",line); 
     printf("MainArray:: %s\n\n",mainArray); 
    } 
    /* Get the number of elements */ 
    for(i=0;i<strlen(mainArray);i++) 
    { 
     if(mainArray[i]==',') 
      count++; 
    } 
    /* Allocate the grid and tokezine */ 
    grid = malloc(sizeof(grid) * count); 
    grid[0] = strdup(strtok(mainArray,",")); 
    for(i=1;i<count;i++) 
    { 
     grid[i] = strdup(strtok(NULL,",")); 
    } 
    /* Display */ 
    for(i=0;i<count;i++) 
    { 
     printf("grid[%2d]:: %s\n",i,grid[i]); 
     free(grid[i]); /* free the malloc-ed string */ 
    } 
    free(grid); 
    fclose(fp); 
    return 0; 
} 

輸出爲您的樣品:

Width: 4, Height: 5 

Line:: 11,12,13,14, MainArray:: 11,12,13,14, 

Line:: 21,22,23,24, MainArray:: 11,12,13,14,21,22,23,24, 

Line:: 31,32,33,34, MainArray:: 11,12,13,14,21,22,23,24,31,32,33,34, 

Line:: 41,42,43,44, MainArray:: 
11,12,13,14,21,22,23,24,31,32,33,34,41,42,43,44, 

Line:: 51,52,53,54, MainArray:: 
11,12,13,14,21,22,23,24,31,32,33,34,41,42,43,44,51,52,53,54, 

Line:: 51,52,53,54, MainArray:: 
11,12,13,14,21,22,23,24,31,32,33,34,41,42,43,44,51,52,53,54,51,52,53,54, 

grid [ 0]:: 11 
grid [ 1]:: 12 
grid [ 2]:: 13 
grid [ 3]:: 14 
grid [ 4]:: 21 
grid [ 5]:: 22 
grid [ 6]:: 23 
grid [ 7]:: 24 
grid [ 8]:: 31 
grid [ 9]:: 32 
grid [10]:: 33 
grid [11]:: 34 
grid [12]:: 41 
grid [13]:: 42 
grid [14]:: 43 
grid [15]:: 44 
grid [16]:: 51 
grid [17]:: 52 
grid [18]:: 53 
grid [19]:: 54 
grid [20]:: 51 
grid [21]:: 52 
grid [22]:: 53 
grid [23]:: 54 
+0

這很奇怪,我可以發誓,我已經評論對這個職位。那麼無論如何... 你好Binayaka!謝謝您的幫助。它適用於:D – mh4

+0

但我有一個問題,雖然@BinayaChakraborty。你爲什麼使用strdup?那和strcpy有什麼區別?我試圖通過研究abt來了解它,但我無法真正掌握「爲什麼」(如果這是有道理的)。 //這是比其他人更有意義的東西。 [link](http://stackoverflow.com/questions/252782/strdup-what-does-it-do-in-c) [link](http://stackoverflow.com/questions/4079850/why- do-we-need-strdup) 我知道它會返回一個字符指針到一個新的「字符串」,它是指向's'(參數)的「字符串」的重複,但爲什麼? – mh4

+0

@ mh4:考慮範圍。如果以後想使用字符串指針,則需要使用有效的返回地址。否則,它可能SEGFAULT –

相關問題