通过从文件中读取将数字提取到字符串数组
Extracting digits to a string array by reading from file
我有一个跟踪文件,其中有 n 行,每行都有序列 read/write,后跟一个十六进制 32 位地址。有点像这样:
read 0x20000064
write 0x20000068
read 0x2000006c
我无法从文件的每一行中仅提取 32 位地址和 read/write 例如 20000064
。所以我打算从文件 trace 中逐行读取,然后将每一行拆分为两个子字符串并根据子字符串采取一些操作。
// Read the second part of sub-string and store as str1
address = str1 & 0xffff;
// read first part of sub-string and store as str0
if (str0 == read)
read_count++;
else if (str0 == write)
write_count++;
所以,简而言之,我坚持将字符串分成两部分并对其进行处理。我已经尝试了从 strtok()
到 fseek()
的所有方法,但没有任何效果。
我有包含以下内容的跟踪文件
read 0x20000064
write 0x20000084
read 0x20000069
write 0x20000070
read 0x20000092
以及我试过的代码
#include <stdio.h>
#include <string.h>
int main() {
FILE *fp;
int i = 0, j = 0, k = 0, n;
char *token[30];
char delim[] = " ";
char str[17], str2[17];
char str3[8];
fp = fopen("text", "r");
while(fgets(str, 17, fp) != NULL) {
fseek(fp, 0, SEEK_CUR);
strcpy(str2, str);
i = 9;
n = 8;
while (str[i] !='[=12=]' && n >= 0) {
str3[j] = str2[i];
i++;
j++;
n--;
}
str3[j] = '[=12=]';
printf("%s\n", str3);
}
fclose(fp);
}
P.S 此代码有效,但仅适用于第一行,之后出现分段错误。
#include <stdio.h>
#include <string.h>
int main() {
FILE *fp;
int i;
char *token[30];
char delim[] = " ";
char str[17], str2[100][17];
char str3[17];
int n = 9, pos = 0;
fp = fopen("text", "r");
while (fgets(str, 17, fp) != NULL) {
fseek(fp, 0, SEEK_CUR);
strcpy(str2[i], str);
if ((n + pos - 1) <= strlen(str2[i])) {
strcpy(&str2[i][pos - 1], &str2[i][n + pos - 1]);
printf("%s\n", str2[i]);
}
i++;
}
fclose(fp);
}
我得到的输出:
20000064
Segmentation fault
我期望的输出:
20000064
20000084
20000069
20000070
20000092
你应该用 sscanf()
:
解析行
#include <stdio.h>
int main() {
char line[80];
char command[20];
unsigned long address;
FILE *fp;
if ((fp = fopen("text", "r")) == NULL)
printf("cannot open text file\n");
return 1;
}
while (fgets(line, sizeof line, fp) != NULL) {
if (sscanf(line, "%19s%x", command, &address) == 2) {
printf("command=%s, address=%x\n", command, address);
}
}
fclose(fp);
return 0;
}
我有一个跟踪文件,其中有 n 行,每行都有序列 read/write,后跟一个十六进制 32 位地址。有点像这样:
read 0x20000064
write 0x20000068
read 0x2000006c
我无法从文件的每一行中仅提取 32 位地址和 read/write 例如 20000064
。所以我打算从文件 trace 中逐行读取,然后将每一行拆分为两个子字符串并根据子字符串采取一些操作。
// Read the second part of sub-string and store as str1
address = str1 & 0xffff;
// read first part of sub-string and store as str0
if (str0 == read)
read_count++;
else if (str0 == write)
write_count++;
所以,简而言之,我坚持将字符串分成两部分并对其进行处理。我已经尝试了从 strtok()
到 fseek()
的所有方法,但没有任何效果。
我有包含以下内容的跟踪文件
read 0x20000064 write 0x20000084 read 0x20000069 write 0x20000070 read 0x20000092
以及我试过的代码
#include <stdio.h>
#include <string.h>
int main() {
FILE *fp;
int i = 0, j = 0, k = 0, n;
char *token[30];
char delim[] = " ";
char str[17], str2[17];
char str3[8];
fp = fopen("text", "r");
while(fgets(str, 17, fp) != NULL) {
fseek(fp, 0, SEEK_CUR);
strcpy(str2, str);
i = 9;
n = 8;
while (str[i] !='[=12=]' && n >= 0) {
str3[j] = str2[i];
i++;
j++;
n--;
}
str3[j] = '[=12=]';
printf("%s\n", str3);
}
fclose(fp);
}
P.S 此代码有效,但仅适用于第一行,之后出现分段错误。
#include <stdio.h>
#include <string.h>
int main() {
FILE *fp;
int i;
char *token[30];
char delim[] = " ";
char str[17], str2[100][17];
char str3[17];
int n = 9, pos = 0;
fp = fopen("text", "r");
while (fgets(str, 17, fp) != NULL) {
fseek(fp, 0, SEEK_CUR);
strcpy(str2[i], str);
if ((n + pos - 1) <= strlen(str2[i])) {
strcpy(&str2[i][pos - 1], &str2[i][n + pos - 1]);
printf("%s\n", str2[i]);
}
i++;
}
fclose(fp);
}
我得到的输出:
20000064
Segmentation fault
我期望的输出:
20000064
20000084
20000069
20000070
20000092
你应该用 sscanf()
:
#include <stdio.h>
int main() {
char line[80];
char command[20];
unsigned long address;
FILE *fp;
if ((fp = fopen("text", "r")) == NULL)
printf("cannot open text file\n");
return 1;
}
while (fgets(line, sizeof line, fp) != NULL) {
if (sscanf(line, "%19s%x", command, &address) == 2) {
printf("command=%s, address=%x\n", command, address);
}
}
fclose(fp);
return 0;
}