我的任务是从.c文件中删除所有注释,并将内容保存在另一个.o文件中。
提供文件:
// Sums two integers.
// Parameters: a, the first integer; b the second integer.
// Returns: the sum.
int add(int a, int b)
{
return a + b; // An inline comment.
}
应该像这样:
int add(int a, int b)
{
return a + b;
}
我已经尝试了多次,但是达到了这种状态:
#include <stdio.h>
int main(int argc, char **argv)
{
FILE * fPtr;
fPtr = fopen("test.o", "w");
char line[300];
FILE *file = fopen("math_functions.c", "r");
if (file == NULL) {
printf("Error: Could not open %s!\n", "math_functions.c");
return -1;
}
else {
while(fgets(line, 300, file)) {
int len = strlen(line);
char helperLineArray[300];
for (int i = 1; i < len; i++) {
if (line[i] == '/' && line[i-1] == '/') {
break;
}
else
{
helperLineArray[i-1] = line[i-1];
}
}
fputs(helperLineArray, fPtr);
}
}
return 0;
}
提前谢谢!
答案 0 :(得分:0)
我认为这会有所帮助
FILE * fPtr;
fPtr = fopen("test.o", "w");
char line[256],helperLineArray[10000];
FILE *file = fopen("math_functions.c", "r");
int j=0;
while (fgets(line, sizeof(line), file)){
int len = strlen(line);
for (int i = 0; i < len-1; i++) {
if (line[i] == '/' && line[i+1] == '/') {
break;
}
else
{
helperLineArray[j++] = line[i];
}
}
helperLineArray[j++] = '\n';
}
fputs(helperLineArray, fPtr);
return 0;