1

我正在尝试从文件中读取并strtok()用来分解我从文件中获取的字符串。问题是,每当我编译程序时,我都会收到这个错误。

source.cpp: In function ‘int main(int, char**)’:
source.cpp:39:32: error: no matching function for call to ‘getline(char [1000], int,     char)’
source.cpp:39:32: note: candidates are:
/usr/include/stdio.h:675:20: note: __ssize_t getline(char**, size_t*, FILE*)
/usr/include/stdio.h:675:20: note:   no known conversion for argument 1 from ‘char   [1000]’ to ‘char**’
/usr/include/c++/4.6/bits/basic_string.h:2734:5: note: template<class _CharT, class    _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>&    std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits,   _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.tcc:1070:5: note: template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>&   std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits,   _Alloc>&, _CharT)

这是导致问题的代码部分。

char *p, line[1000], opcode[9], arg1[256], arg2[256];

int i = 0;

while(getline(line, 1000, '\n') != NULL)
{
    line[strlen(line)-1] = '\0';
    cout << "Line = " << line << endl;

    if (strchr(line, '#'))
    {
        *p = '\0';
    }
    if (p = strtok(line, "\t"))
        strcpy(opcode,p);
    if (p = strtok(NULL, "\t"))
        strcpy(arg1,p);
    if (p = strtok(NULL, "\t"))
        strcpy(arg2,p);

    printf("opcode=:%s: arg1=:%s: arg2=:%s:\n",opcode,arg1,arg2);

}

我会很感激我能得到的任何帮助。谢谢你。

4

2 回答 2

0

您完全错误地调用该getline()函数。getline()有关如何使用它,请参阅手册。你得到一个提示:

candidates are:
__ssize_t getline(char**, size_t*, FILE*)

在你的情况下,它会是这样的:

getline(&line, 1000, your_file_pointer)
于 2012-09-14T12:18:24.420 回答
0

您需要先打开文件的流,然后才能开始写入。

这是有关如何执行此操作的教程。

于 2012-09-14T12:19:39.240 回答