我有一个带有#个符号和整数的文件。我想避免读#号并计算每行整数的数量。我是C语言的新手,不胜感激任何提示和帮助!
这是我到目前为止所拥有的:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_NUM 1000
#define MAX_LINE_LEN 2048
int main(int argc, char *argv[]) {
FILE *fp;
char line[MAX_LINE_LEN], *p;
int i, array[MAX_NUM] = { 0 }, count = 0;
char filename[100];
printf("Enter the file name: \n");
scanf("%s", filename);
fp = fopen(filename,"r");
while (((p = fgets(line, MAX_LINE_LEN, fp)) != NULL) && (count < MAX_NUM)) {
if (strlen(line) > 1) {
//counter for line number.
count++;
//this line does not contains numbers.
if (line[0] == '#')continue;
do {
//notice. this way we count 111 as one number instead of 3.
if (isdigit((int)*p) && (*p == ' ' || *p == '\0')) {
array[count] ++;
}
} while (*p++ != '\0');
}
//printf("array[]: %d", array[i]);
int i;
for(i = 0; i < count; ++ i){
printf("%d",array[i]);
}
}
return(0);
}
这是我想您尝试做的事情的我的版本-计算整数,而不是单个数字。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX_NUM 1000
#define MAX_LINE_LEN 2048
void fatal(char *msg) {
printf("%s\n", msg);
exit (1);
}
int main(int argc, char *argv[]) {
FILE *fp;
char filename[100];
char line[MAX_LINE_LEN];
char *p;
int i;
int array[MAX_NUM] = { 0 };
int count = 0;
printf("Enter the file name: \n");
if (scanf("%s", filename) != 1)
fatal("Bad filename entry");
if ((fp = fopen(filename,"r")) == NULL)
fatal("Unable to open the file");
while ((p = fgets(line, MAX_LINE_LEN, fp)) != NULL) {
if (count >= MAX_NUM)
fatal("Broke the array");
if (line[0] != '#') {
p = strtok(line, " ");
while (p != NULL) {
if(isdigit(*p))
array[count]++;
p = strtok(NULL, " ");
}
}
count++;
}
for(i = 0; i < count; i++) {
if(array[i]) {
printf("Integers: %d\n", array[i]);
}
}
return 0;
}
程序输出:
Enter the file name:
test.txt
Integers: 9
Integers: 4
Integers: 5
Integers: 5
Integers: 5
Integers: 1
Integers: 20
我想我知道你在做什么。您正在读取文件,跳过每条blank或comment行,然后计算在每行中找到的整数数,并将整数计数(每行)保存在array[]
中。您走在正确的轨道上,只是打了些小故障。
虽然您可以使用各种string.h
和ctype.h
工具来解决问题,但有时最好只使用一个指针并一次沿着每一行向下移动一个字符。您已经创建了指针p
,可用于查找每行中整数出现的次数。
最简单的方法是检查line
中的第一个字符(因此为*p
)。如果等于newline
或'#'
,则该行为空或comment-跳过该行。接下来,对于该行中的每个字符,您都想前进p
直到找到一个数字。您可以使用isdigit
,也可以使用:
while (*p && (*p < '0' || '9' < *p)) p++;
找到数字后,增加整数计数:
if ('0' <= *p && *p <= '9') array[count]++;
([note:它是array[count]++;
而不是array[count++];
)
然后简单地将整数中的所有数字都移到前面,当您到达下一个非数字字符时,重复循环:
while (*p && ('0' <= *p && *p <= '9')) p++;
到达行尾时,递增count
(这是您的line-counter,而不是整数),然后转到下一行。
将所有片段放在一起,您可以执行以下操作。注意:程序期望将文件名作为第一个参数,或者,如果没有给出参数,则默认情况下它将从stdin
中读取:
#include <stdio.h>
enum { MAX_NUM = 1000, MAX_LINE_LEN = 2048 };
int main(int argc, char **argv) {
char line[MAX_LINE_LEN], *p;
int i, array[MAX_NUM] = { 0 }, count = 0;
FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;
if (!fp) { /* validate the file is open */
fprintf (stderr, "error: file open failed '%s'.\n", argv[1]);
return 0;
}
while (count < MAX_NUM && (p = fgets(line, MAX_LINE_LEN, fp))) {
if (*p == '\n' || *p == '#') { count++; continue; }
while (*p) { /* for each char in line */
while (*p && (*p < '0' || '9' < *p)) p++;
if ('0' <= *p && *p <= '9') array[count]++;
while (*p && ('0' <= *p && *p <= '9')) p++;
}
count++; /* increment line count */
}
if (fp != stdin) fclose(fp);
printf ("\nThe lines from '%s' contained the following integers:\n\n",
argc > 1 ? argv[1] : "stdin");
for (i = 0; i < count; i++)
printf (" line[%3d] : %d\n", i, array[i]);
putchar ('\n');
return(0);
}
样本输入
$ cat ../dat/commentint.txt
# each burst is one long
1 2 3 4 5 6 5 4 5
14 62 48 14
# these have 5 integers
1 3 5 7 9
123 456 789 1234 5678
34 34 34 34 34
# special case, I guess
1
# now a burstful sequence
1 2 2 2 2 2 3 3 4 4 4 4 5 5 6 7 7 7 1 1
示例使用/输出
$ ./bin/skipcomment ../dat/commentint.txt
The lines from '../dat/commentint.txt' contained the following integers:
line[ 0] : 0
line[ 1] : 9
line[ 2] : 4
line[ 3] : 0
line[ 4] : 0
line[ 5] : 5
line[ 6] : 5
line[ 7] : 5
line[ 8] : 0
line[ 9] : 0
line[ 10] : 1
line[ 11] : 0
line[ 12] : 0
line[ 13] : 20
让我知道是否有问题。
仅找到具有整数的打印行
仅显示带有整数的行,只需在打印循环中添加if
语句即可。例如:]]
for (i = 0; i < count; i++) if (array[i]) printf (" line[%3d] : %d\n", i, array[i]);
输出
$ ./bin/skipcomment ../dat/commentint.txt
The lines from '../dat/commentint.txt' contained the following integers:
line[ 1] : 9
line[ 2] : 4
line[ 5] : 5
line[ 6] : 5
line[ 7] : 5
line[ 10] : 1
line[ 13] : 20
问题是此行:
您为什么使用以下值: