gpt4 book ai didi

c - 用C从文件中读取字符和字符串

转载 作者:行者123 更新时间:2023-11-30 21:42:27 24 4
gpt4 key购买 nike

我正在开发一个项目,其中我将有读取或写入命令,然后在文件中的一行上有一个地址。格式如下:

R 0x...
W 0x...

文件有数千行长。我正在尝试读取命令并将其放入 buf1 并将地址读取到 buf2 中。我尝试使用 fgets/fgetc 以及 fscanf"%*c %s" 以及相反的方式来执行此操作。每次我这样做时,buf2 都会正确地获取地址,但命令非常不稳定。这是我的代码:

char buf1, buf2[64];
int readcount = 0, writecount = 0, other = 0;
while(!feof(trace)){
printf("\nFile is open");
fgetc(trace);
fgets(buf2,16,trace);
printf("\nBuf1 is: %c",buf1);
printf("\nBuf2 is: %s",buf2);

我不断得到的输出是:

The address is: E  
File is open
Buf1 is: ?
Buf2 is: 0x123456

The address is: V
File is open
Buf1 is: ?
Buf2 is: 0x234567

The address is: g
File is open
Buf1 is: ?
Buf2 is: 0x345678

The address is: x
File is open
Buf1 is: ?
Buf2 is: 0x345678

我感觉问题出在我对文件读取功能的理解上。为什么 buf1 读取不正确?

最佳答案

(我这样做的时候 R 0x...W 0x... 是在同一行而不是两个上给出的)

如果您确定文件的每一行都包含 R<space>0x...<space>W<space>0x...那么你可以这样做

int main()
{
FILE * fp = fopen("in", "r");

if (fp == NULL) {
puts("cannot open 'in'");
return -1;
}

char line[100];

while (fgets(line, sizeof(line), fp)) {
char c1, c2;
unsigned n1, n2;

errno = 0;
if ((sscanf(line, "%c %x %c %x", &c1, &n1, &c2, &n2) == 4) && !errno) {
// just to indicate it read well
printf("%c:%x:%c:%x\n", c1, n1, c2, n2);
}
else {
printf("invalid line %s\n", line);
}
}

puts("done");
fclose(fp);
}

编译与执行

pi@raspberrypi:/tmp $ gcc -pedantic -Wextra f.c
pi@raspberrypi:/tmp $ cat in
R 0x1 W 0x2
A 0x123 Z 0x345
C 0x0 Z 0x678
pi@raspberrypi:/tmp $ ./a.out
R:1:W:2
A:123:Z:345
C:0:Z:678
done

请注意,我首先阅读该行,以免受到换行符等的干扰

<小时/>

如果字段之间可以有多个空格和/或制表符和/或什至在行的开头,您可以使用 strtok :

#include <stdio.h>
#include <string.h>
#include <errno.h>

int getChar(char * c, char * s, int n)
{
if (s == NULL) {
printf("field %d is missing\n", n);
return 0;
}
if (s[1] != 0) {
printf("invalid field %d : '%s'\n", n, s);
return 0;
}

*c = s[0];
return 1;
}

int getUnsigned(unsigned * u, char * s, int n)
{
if (s == NULL) {
printf("field %d is missing\n", n);
return 0;
}

char c;

errno = 0;
if ((sscanf(s, "%x%c", u, &c) != 1) || (errno != 0)) {
printf("invalid field %d : '%s'\n", n, s);
return 0;
}

return 1;
}

int main()
{
FILE * fp = fopen("in", "r");

if (fp == NULL) {
puts("cannot open 'in'");
return -1;
}

char line[100];

while (fgets(line, sizeof(line), fp)) {
char c1, c2;
unsigned n1, n2;

if (getChar(&c1, strtok(line, " \t"), 0) &&
getUnsigned(&n1, strtok(NULL, " \t"), 1) &&
getChar(&c2, strtok(NULL, " \t"), 2) &&
getUnsigned(&n2, strtok(NULL, " \t\n"), 3)) /* warning \n is added */
// just to indicate it read well
printf("%c:%x:%c:%x\n", c1, n1, c2, n2);
}

puts("done");
fclose(fp);
}

编译和执行:

pi@raspberrypi:/tmp $ gcc -pedantic -Wextra ff.c
pi@raspberrypi:/tmp $ cat in
R 0x1 W 0x2
A 0x123 Z 0x345
C 0x0 Z 0x678
pi@raspberrypi:/tmp $ ./a.out
R:1:W:2
A:123:Z:345
C:0:Z:678
done

关于c - 用C从文件中读取字符和字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55107830/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com