getline()返回错误

时间:2012-09-14 12:15:09

标签: c++

我正在尝试从文件中读取并使用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);

}

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

2 个答案:

答案 0 :(得分:0)

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

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

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

getline(&line, 1000, your_file_pointer)

答案 1 :(得分:0)

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

Here is a tutorial on how to do it.