如何从文件中读取字符串并将其tokenise到一个数组? [英] How to read string from file and tokenise it into an array?
问题描述
我的问题是:我如何读一个字符串,在多条线路从在C文件中的字符串的一行?我试图做一个二进制的地图,我的一维数组重新psented作为一个二维数组$ P $。因此,这是在我的level_1.txt
My question is: How do i read a string that is on multiple lines as one line of string from a file in C? I'm trying to do a binary map and my 1d array is represented as a 2d array. So this is in my "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 ......
and i would like to get the string "11,12,13,14,21,22...."
这是我的code:
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行的规模和使用这些值来创建一维数组什么。然后,一旦这样做了,我想读其余字符串,并将其分配给数组。例如:
What i want to do is read the file, get the size from the 1st 2 lines and use those values to create the 1d array. Then once that's done, i want to read the remaining string and assign it to the array. Eg.
theScreenMap[0] = 11; // first element has 1st token
theScreenMap[1] = 12;
theScreenMap[size - 1] = 54; // last element has last token
由于任何人帮助我。但是,如果任何人有这样做的更好的方法(从文件读取和初始化数组创建二进制地图),然后随时告诉我的。谢谢! :)
Thanks to anyone helping me out. But if anyone has a better way of doing this (read from file and init an array to create binary map), then feel free to tell me so. Thanks! :)
推荐答案
这仅仅是一个基本的实现你想要什么。最终的结果将是许多行作为高度,以及将它们连接起来。
This is just a basic implementation of what you want. The end result would read as many lines as 'height', and concatenate them.
#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;
}
为您的样品输出:
Output for your sample:
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
这篇关于如何从文件中读取字符串并将其tokenise到一个数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!