从文件中获取一个字符后的整数
Get integer after a character from file
我正在尝试读取这样的 txt 文件的编号:
input=20
output=10
hidden=5
....
我试过这个代码:
char line[30];
char values[100][20];
int i = 0;
FILE *fp;
fp = fopen("myFile.txt", "r");
if(fp == NULL)
{
printf("cannot open file\n");
return 0;
}
while(fgets(line, sizeof(line), fp) != NULL)
{
sscanf(line, "%[^=]", values[i])
printf("%s\n", values[i]);
i++;
}
fclose(fp);
但我只获得了第一个单词,而没有获得 =
之后的数字。
我明白了
input
output
etc
而不是
20
10
5
etc
如何获取号码?
不要为此使用 sscanf()
,重新声明 values
来存储像
这样的整数
int values[LARGE_CONSTANT_NUMBER];
并且在 fgets()
之后使用 strchr
char *number;
number = strchr(line, '=');
if (number == NULL)
continue;
number += 1;
values[i] = strtol(number, NULL, 10);
如果愿意,您也可以使用 malloc()
和 realloc()
来使 values
数组动态化。
喜欢就试试
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char line[100];
int values[100];
int i;
FILE *fp;
size_t maxIntegers;
fp = fopen("myFile.txt", "r");
if (fp == NULL)
{
perror("cannot open file\n");
return 0;
}
i = 0;
maxIntegers = sizeof(values) / sizeof(values[0]);
while ((fgets(line, sizeof(line), fp) != NULL) && (i < maxIntegers))
{
char *number;
number = strchr(line, '=');
if (number == NULL) /* this line does not contain a `=' */
continue;
values[i++] = strtol(number + 1, NULL, 10);
printf("%d\n", values[i - 1]);
}
fclose(fp);
return 0;
}
使用此技术,您可以避免将数字不必要地存储为字符串。
这一行
sscanf(line, "%[^=]", values[i]);
表示"read everything up to, but not including, the =
sign into values[i]
"。
如果您对等号后的数字部分感兴趣,请按如下方式更改调用:
sscanf(line, "%*[^=]=%19s", values[i]);
这个格式行表示"read and ignore (because of the asterisk) everything up to, and including, the equal sign. Then read a string of length of up to 19 characters into values[i]
".
我正在尝试读取这样的 txt 文件的编号:
input=20
output=10
hidden=5
....
我试过这个代码:
char line[30];
char values[100][20];
int i = 0;
FILE *fp;
fp = fopen("myFile.txt", "r");
if(fp == NULL)
{
printf("cannot open file\n");
return 0;
}
while(fgets(line, sizeof(line), fp) != NULL)
{
sscanf(line, "%[^=]", values[i])
printf("%s\n", values[i]);
i++;
}
fclose(fp);
但我只获得了第一个单词,而没有获得 =
之后的数字。
我明白了
input
output
etc
而不是
20
10
5
etc
如何获取号码?
不要为此使用 sscanf()
,重新声明 values
来存储像
int values[LARGE_CONSTANT_NUMBER];
并且在 fgets()
之后使用 strchr
char *number;
number = strchr(line, '=');
if (number == NULL)
continue;
number += 1;
values[i] = strtol(number, NULL, 10);
如果愿意,您也可以使用 malloc()
和 realloc()
来使 values
数组动态化。
喜欢就试试
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char line[100];
int values[100];
int i;
FILE *fp;
size_t maxIntegers;
fp = fopen("myFile.txt", "r");
if (fp == NULL)
{
perror("cannot open file\n");
return 0;
}
i = 0;
maxIntegers = sizeof(values) / sizeof(values[0]);
while ((fgets(line, sizeof(line), fp) != NULL) && (i < maxIntegers))
{
char *number;
number = strchr(line, '=');
if (number == NULL) /* this line does not contain a `=' */
continue;
values[i++] = strtol(number + 1, NULL, 10);
printf("%d\n", values[i - 1]);
}
fclose(fp);
return 0;
}
使用此技术,您可以避免将数字不必要地存储为字符串。
这一行
sscanf(line, "%[^=]", values[i]);
表示"read everything up to, but not including, the =
sign into values[i]
"。
如果您对等号后的数字部分感兴趣,请按如下方式更改调用:
sscanf(line, "%*[^=]=%19s", values[i]);
这个格式行表示"read and ignore (because of the asterisk) everything up to, and including, the equal sign. Then read a string of length of up to 19 characters into values[i]
".