C 如何从文件中读取字符串并将其标记到数组中?
我的问题是:如何从C语言的文件中读取多行字符串作为一行字符串?我试着做一个二进制映射,我的一维数组表示为二维数组。这就是我的“level_1.txt”: 我想得到字符串“11,12,13,14,21,22…” 这是我的代码:C 如何从文件中读取字符串并将其标记到数组中?,c,file-io,maps,C,File Io,Maps,我的问题是:如何从C语言的文件中读取多行字符串作为一行字符串?我试着做一个二进制映射,我的一维数组表示为二维数组。这就是我的“level_1.txt”: 我想得到字符串“11,12,13,14,21,22…” 这是我的代码: int ImportMapDataFromFile(char *fileName, Map *self) { FILE *pFile; char* myStr; pFile = fopen(fileName, "r"); // Check
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;
}
}
我要做的是读取文件,从前两行获取大小,并使用这些值创建1d数组。完成后,我想读取剩余的字符串并将其分配给数组。例如
theScreenMap[0] = 11; // first element has 1st token
theScreenMap[1] = 12;
theScreenMap[size - 1] = 54; // last element has last token
感谢所有帮助我的人。但如果有人有更好的方法(从文件中读取并初始化一个数组来创建二进制映射),请随时告诉我。谢谢!:) 这只是您想要的基本实现。最终结果将读取与“height”一样多的行,并将它们连接起来
#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;
}
真奇怪,我发誓我已经对这篇文章发表了评论。不管怎样。。。你好,比纳亚卡!谢谢你的帮助。但我有一个问题@Binayacharaborty。你为什么用strdup?那和strcpy有什么区别?我试图通过研究abt来理解它,但我无法真正理解“为什么”(如果这有道理的话)。//这些对我来说比其他的更有意义。我知道它将返回一个指向新“字符串”的char指针,该字符串是指向“s”(参数)的“字符串”的副本,但为什么?@mh4:请考虑范围。如果以后要使用字符串指针,则必须使用有效的返回地址。否则,它可能会出错哦,好的!谢谢,这帮了大忙!
#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