通读文本文件并在每次遇到等号时递增一个变量
Read through a text file and increment a variable every time an equals sign is encountered
我正在尝试编写一个程序,它将读取一个 makefile,并在每次在文件中遇到等号“=”时递增一个计数器。这是我对这样一个程序的尝试(递增不是这个程序的唯一目的,它恰好是我目前卡住的点):
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <stdbool.h>
struct VarMap {
int data;
int key;
};
// Use a hash table to store the variables - variablename : definition
void processFile(FILE* spData) {
int varCount = 0;
char buffer[1000];
while (fgets(buffer , sizeof(buffer) , spData) != NULL) {
if (buffer[0] == '#') continue;
for (int i = 0; i != '[=10=]' ; i++) {
if (buffer[i] == '=') {
varCount++;
continue;
}
}
}
printf ("varCount has counted %d equals signs.\n\n" , varCount);
// This will hold the variables
struct VarMap variables[varCount];
}
int main(int argc, const char * argv[]) {
char filepath[1000];
printf("Enter the filepath of the Bakefile or bakefile: ");
scanf("%s" , filepath);
FILE* spData = fopen(filepath , "r");
if (spData == NULL) {
printf ("Cannot open file.");
exit(EXIT_FAILURE);
}
processFile(spData);
fclose(spData);
return 0;
}
我们感兴趣的函数是processFile函数。我的推理流程是逐行读取文件到数组 缓冲区 ,然后解析数组直到找到第一个等号,我将在此处递增 varCount 和 continue
到下一行。然后我可以使用这个变量来初始化一个键映射来存储与变量名称及其内容对应的值对。
我的问题是,程序一直返回 0 个等号 ,每当我 运行 它并输入具有以下内容的文件时(显然存在等号但没有被拾取):
calcmarks : calcmarks.o globals.o readmarks.o correlation.o
cc -std=c99 -Wall -pedantic -Werror -o calcmarks \
calcmarks.o globals.o readmarks.o correlation.o -lm
calcmarks.o : calcmarks.c calcmarks.h
cc -std=c99 -Wall -pedantic -Werror -c calcmarks.c
globals.o : globals.c calcmarks.h
cc -std=c99 -Wall -pedantic -Werror -c globals.c
readmarks.o : readmarks.c calcmarks.h
cc -std=c99 -Wall -pedantic -Werror -c readmarks.c
correlation.o : correlation.c calcmarks.h
cc -std=c99 -Wall -pedantic -Werror -c correlation.c
clean:
rm -f *.o calcmarks
您可能已经猜到了,我正在尝试用 C 编写一个可以处理 Makefile 的程序的实现!可悲的是,这不是一件容易的事。
所以我的问题是;
我做错了什么/遗漏了什么阻止 varCount 递增?
for循环的测试条件应该是:
buffer[i] != '[=10=]'
感谢@achal 指出。
我正在尝试编写一个程序,它将读取一个 makefile,并在每次在文件中遇到等号“=”时递增一个计数器。这是我对这样一个程序的尝试(递增不是这个程序的唯一目的,它恰好是我目前卡住的点):
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <stdbool.h>
struct VarMap {
int data;
int key;
};
// Use a hash table to store the variables - variablename : definition
void processFile(FILE* spData) {
int varCount = 0;
char buffer[1000];
while (fgets(buffer , sizeof(buffer) , spData) != NULL) {
if (buffer[0] == '#') continue;
for (int i = 0; i != '[=10=]' ; i++) {
if (buffer[i] == '=') {
varCount++;
continue;
}
}
}
printf ("varCount has counted %d equals signs.\n\n" , varCount);
// This will hold the variables
struct VarMap variables[varCount];
}
int main(int argc, const char * argv[]) {
char filepath[1000];
printf("Enter the filepath of the Bakefile or bakefile: ");
scanf("%s" , filepath);
FILE* spData = fopen(filepath , "r");
if (spData == NULL) {
printf ("Cannot open file.");
exit(EXIT_FAILURE);
}
processFile(spData);
fclose(spData);
return 0;
}
我们感兴趣的函数是processFile函数。我的推理流程是逐行读取文件到数组 缓冲区 ,然后解析数组直到找到第一个等号,我将在此处递增 varCount 和 continue
到下一行。然后我可以使用这个变量来初始化一个键映射来存储与变量名称及其内容对应的值对。
我的问题是,程序一直返回 0 个等号 ,每当我 运行 它并输入具有以下内容的文件时(显然存在等号但没有被拾取):
calcmarks : calcmarks.o globals.o readmarks.o correlation.o
cc -std=c99 -Wall -pedantic -Werror -o calcmarks \
calcmarks.o globals.o readmarks.o correlation.o -lm
calcmarks.o : calcmarks.c calcmarks.h
cc -std=c99 -Wall -pedantic -Werror -c calcmarks.c
globals.o : globals.c calcmarks.h
cc -std=c99 -Wall -pedantic -Werror -c globals.c
readmarks.o : readmarks.c calcmarks.h
cc -std=c99 -Wall -pedantic -Werror -c readmarks.c
correlation.o : correlation.c calcmarks.h
cc -std=c99 -Wall -pedantic -Werror -c correlation.c
clean:
rm -f *.o calcmarks
您可能已经猜到了,我正在尝试用 C 编写一个可以处理 Makefile 的程序的实现!可悲的是,这不是一件容易的事。
所以我的问题是;
我做错了什么/遗漏了什么阻止 varCount 递增?
for循环的测试条件应该是:
buffer[i] != '[=10=]'
感谢@achal 指出。