从C中的文件中读取字符和字符串[关闭]

问题描述 投票:-5回答:1

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

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

而且文件长达数千行。我试图读取命令并将其放入buf1并将地址读入buf2。我试图用fgets / fgetcfscanf"%*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读取不正确?

c c-strings file-processing
1个回答
2
投票

(我做了那个答案,而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
© www.soinside.com 2019 - 2024. All rights reserved.