C 如何从文件中逐行将字和整数写入数组
我有一个文本文件C 如何从文件中逐行将字和整数写入数组,c,arrays,C,Arrays,我有一个文本文件text.txt,其中包含以下数据: David 8 13 Bob 9 15 Dylan 3 18 Andy 4 14 我需要将名称写入一个数组,其他数字也写入各自的数组。这将在以后我展开“程序”功能时用于处理 我有一个程序,将整个文件写入一个字符数组,这是一个开始,但我非常卡住了。我需要将每行的组件写入它们自己的数组 #include <stdio.h> #include <stdlib.h> int main(void) { int i=0
text.txt
,其中包含以下数据:
David 8 13
Bob 9 15
Dylan 3 18
Andy 4 14
我需要将名称写入一个数组,其他数字也写入各自的数组。这将在以后我展开“程序”功能时用于处理
我有一个程序,将整个文件写入一个字符数组,这是一个开始,但我非常卡住了。我需要将每行的组件写入它们自己的数组
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int i=0;
char* string[100];
char line[100];
FILE *file;
file = fopen("text.txt", "r");
while (fgets(line, sizeof line, file) != NULL) { // EDIT: I added a { here!
printf("%s", line);
string[i]=line;
i++;
}
for (i=0 ; i<4; i++) {
printf("\n%s", string[i]);
}
fclose(file);
return 0;
}
在分析字符串之前,代码中有一个严重错误。您没有将字符串正确写入数组 您创建了一个大小为100的(char*)指针数组,因此基本上有100个指向字符串的指针,但没有为字符串分配内存。 然后将该数组中的每个指针指向数组“line”。 基本上,在第一个while循环之后,字符串数组如下所示:
[0] "Andy 4 14"
[1] "Andy 4 14"
[2] "Andy 4 14"
[3] "Andy 4 14"
[4] (trash)
[5] (trash)
...
这是因为它们都指向循环末尾的线数组,该数组包含文件中的最后一行,即“Andy 4 14”
这里要做的是使用malloc(3)为每个字符串动态分配空间,或者像这样声明字符串数组,在堆栈上分配内存:
char strings[100][100];
然后使用FGET写入数组:
#include <stdio.h>
#include <stdlib.h>
#define MAX_STRINGS (100)
#define MAX_STRING_LEN (100)
int main(void) {
int i = 0;
char strings[MAX_STRINGS][MAX_STRING_LEN] = { 0 };
FILE *file = NULL;
file = fopen("text.txt", "r");
if (NULL == file) {
fprintf(stderr, "Failed to open file.");
return -1;
}
for (i = 0; i < MAX_STRINGS; i++) {
if (NULL == fgets(strings[i], MAX_STRING_LEN, file)) {
break;
}
}
for (i = 0; i < MAX_STRINGS; i++) {
printf("%s", strings[i]);
}
fclose(file);
return 0;
}
#包括
#包括
#定义最大字符串数(100)
#定义最大字符串长度(100)
内部主(空){
int i=0;
字符字符串[MAX_strings][MAX_STRING_LEN]={0};
FILE*FILE=NULL;
file=fopen(“text.txt”、“r”);
if(NULL==文件){
fprintf(stderr,“无法打开文件”);
返回-1;
}
对于(i=0;i
因此,如果我理解正确,您需要3个数组:一个用于名称,一个用于第一个整数值,另一个用于第二个整数值。我假设值之间有白色字符(空格、制表符、新行),您可以执行以下操作(您不必阅读整行)(也可以查看@Tal Avraham的aswer!)
首先要做的是缩进代码。好的,有人帮你做了……然后查看
sscanf
函数strtok也可能有用。提示:string[i]=line
不复制字符串,它只复制指针。提示:查看strdup
,或查看malloc
和strcpy
。阅读初学者C课本中关于字符串和指针的章节。@Jabberwocky是一个善良的观察者完成的!(还进行了“打字”更正?)顺便说一句,如果文件访问成功与否,我将实现一个控件:if((file=fopen(“text.txt”),“r”)==0)printf(“打开文件时出错”);else{如果文件已成功打开读取,则返回代码}
#include <stdio.h>
#include <stdlib.h>
#define MAX_STRINGS (100)
#define MAX_STRING_LEN (100)
int main(void) {
int i = 0;
char strings[MAX_STRINGS][MAX_STRING_LEN] = { 0 };
FILE *file = NULL;
file = fopen("text.txt", "r");
if (NULL == file) {
fprintf(stderr, "Failed to open file.");
return -1;
}
for (i = 0; i < MAX_STRINGS; i++) {
if (NULL == fgets(strings[i], MAX_STRING_LEN, file)) {
break;
}
}
for (i = 0; i < MAX_STRINGS; i++) {
printf("%s", strings[i]);
}
fclose(file);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define MAX_STRINGS (10)
#define MAX_STRING_LEN (100)
int main()
{
char names[MAX_STRINGS][MAX_STRING_LEN] = { 0 };
int firstValues[MAX_STRINGS] = { 0 };
int secondValues[MAX_STRINGS] = { 0 };
FILE* file = fopen("text.txt", "r"); // check if open was successful!
int i;
for (i = 0; i < MAX_STRINGS; ++i)
{
if (!fscanf(file, "%s", names[i])) //read the name as string
break;
fscanf(file, "%d", &firstValues[i]); //read the 1st value as integer
fscanf(file, "%d", &secondValues[i]); //read the 2nd value as integer
}
fclose(file);
for (i = 0; i < MAX_STRINGS; ++i)
printf("%s %d %d\n", names[i], firstValues[i], secondValues[i]);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define MAX_STRINGS (10)
#define MAX_STRING_LEN (100)
typedef struct
{
char name[MAX_STRING_LEN];
int firstValue;
int secondValue;
} ValuesType;
int main()
{
ValuesType dataArray[MAX_STRINGS] = { 0 };
FILE* file = fopen("text.txt", "r"); // check if open was successful!
int i;
for (i = 0; i < MAX_STRINGS; ++i)
{
if (!fscanf(file, "%s", dataArray[i].name)) //read the name as string
break;
fscanf(file, "%d", &dataArray[i].firstValue); //read the 1st value as integer
fscanf(file, "%d", &dataArray[i].secondValue); //read the 2nd value as integer
}
fclose(file);
for (i = 0; i < MAX_STRINGS; ++i)
{
printf("%s %d %d\n", dataArray[i].name, dataArray[i].firstValue, dataArray[i].secondValue);
}
return 0;
}