第二次编辑:
谢谢大家迄今为止的帮助!我已经用纯 c 语言纠正了所有内容,因为我的作业必须用它来编写。显然,我对 C 的了解很糟糕。当我写这篇文章时,我实际上是在自学 C,但使用了我所有的 C++ 知识。因此我对语言文字感到困惑。不过,我的 firstCheck() 函数仍然存在问题。下面我将直接用我当前的代码进行解释。
firstCheck() 函数无法正常工作。在 readFile() 函数中,我已成功将给定文件中的文本逐行拆分为数组。 然后,firstCheck() 应该采用该数组“myString”并读取第一个字符串,直到出现“”(基本上是第一个单词/字符/等),以便我可以对其进行评估。
更新了 Assem.c
#include "assem.h"
int readFile(FILE *file, struct assem *item)
{
size_t i =0;
item->counter = 0; //this is sort of the constructor, if you will.
size_t maxLines = sizeof item->myLines / sizeof *item->myLines;
//breaks down text file into line by line
while(i < maxLines && fgets(item->myString, sizeof item->myString, file)) //and stores them into "myLines array"
{
item->myLines[i] = strdup(item->myString);
i++;
item->counter++;
}
return 0;
}
void printFile(struct assem item) //just printing things..prints it with a new line in front
{
printf("\n");
for(int s = 0; s < item.counter; s++)
{
printf("%s\n", item.myLines[s]);
}
printf("\n");
}
int firstCheck(struct assem item)
{
char *myWord [7] = {NULL};
for(int s = 0; s < item.counter; s++)
{
while(strcmp( fgets( item.myString, sizeof item.myString, stdin ), " " ) != 0 )
{
myWord[s] = strdup(item.myLines[s]);
}
}
for(int s = 0; s < item.counter; s++)
{
printf("%s\n", myWord[s]);
}
return 0;
}
更新了“assem.h”
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct assem
{
char myString[101]; //buffer
char *myLines[20]; //this will store the lines from a text file..up to 20 lines
int counter; //counter for number of lines
//printing stuff...prints file directly from whats STORED IN THE ARRAY
};
int readFile(FILE *FileToBeRead, struct assem *item); //takes in the file that needs to be read and splits it into lines
int firstCheck(struct assem item);
void printFile(struct assem item);
更新了“main.c”
#include "Assem.c"
int main()
{
struct assem test;
FILE *mips;
mips = fopen("/home/rpf0024/cse2610/Program1/mips.asm", "r");
readFile(mips, &test);
printFile(test);
firstCheck(test);
fclose(mips);
return 0;
}
第一次编辑旧: 我目前正在编写一些出现错误的代码,但我不确定如何修复它们。这些有什么解释吗? 这些错误出现在我的firstCheck() 函数中,该函数尝试读取字符串,直到*myLines[] 数组中的第一个“”字符为止。基本上,我试图读取第一个单词,直到出现空格,将其存储在另一个数组中,以便我可以对其进行评估。
这里有以下错误: 错误始终与我的 .c 文件底部的 firstCheck() 函数相关联。我对行号进行了注释。
Assem.c:35:31: error: ISO C++ forbids comparison between pointer and integer [-fpermissive] Assem.c:37:31: error: cannot convert ‘char**’ to ‘const char*’ for argument ‘1’ to ‘char* strdup(const char*)
.c文件
#include "assem.h"
int assem::readFile(FILE *file)
{
int i =0;
counter = 0; //this is sort of the constructor, if you will.
//breaks down text file into line by line
while(fgets(myString, 101, file) != NULL) //and stores them into "myLines array"
{
myLines[i] = strdup(myString);
i++;
counter++;
}
return 0;
}
void assem::printFile() //just printing things..prints it with a new line in front
{
printf("\n");
for(int s = 0; s < counter; s++)
{
printf("%s\n", *(myLines+s));
}
printf("\n");
}
int assem::firstCheck()
{
char *myWord [7];
for(int s = 0; s < counter; s++) //33
{
while(gets(*(myLines+s)) != ' ') //35
{
myWord[s] = strdup(myLines); //37
}
}
return 0;
}
.h 文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
class assem
{
public:
char myString[101]; //buffer
char *myLines[20]; //this will store the lines from a text file..up to 20 lines
int counter; //counter for number of lines
int readFile(FILE *FileToBeRead); //takes in the file that needs to be read and splits it into lines
int firstCheck();
void printFile(); //printing stuff...prints file directly from whats STORED IN THE ARRAY
};
最佳答案
忽略gets()
的邪恶 ,下面的行
while(gets(*(myLines+s)) != ' ')
是错误的,也是第一个错误的原因。 gets()
返回一个 char *
,您将其与空格、char
进行比较。这是错误的。
建议:使用fgets()
。你可以使用类似的东西
while (fgets(string, SIZE, stdin))
它检查 fgets()
的返回值反对 NULL,失败指示器。
另一个错误,我相信是因为试图混合 C
和 C++
。使用标准 C
语法重新编写相同的代码,并使用 C 编译器进行编译,就可以了。
关于C 代码的 C++ 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31586245/