Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在Linux中使用Cpp从文本文件打印数据_C++_Linux - Fatal编程技术网

C++ 在Linux中使用Cpp从文本文件打印数据

C++ 在Linux中使用Cpp从文本文件打印数据,c++,linux,C++,Linux,我只想打印出一组整数,在文本文件的每一行写下一个。 因为我使用的是Linux,所以我似乎无法使用像getch()和getline()这样方便的函数 请阅读下面的代码,并告诉我需要更改什么才能看到文本文件中的整数 #include <iostream> #include <fstream> using namespace std; int main() { fstream fp; fp.open("/home/Documents/C/Codeblocks/t

我只想打印出一组整数,在文本文件的每一行写下一个。 因为我使用的是Linux,所以我似乎无法使用像getch()和getline()这样方便的函数

请阅读下面的代码,并告诉我需要更改什么才能看到文本文件中的整数

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    fstream fp;
    fp.open("/home/Documents/C/Codeblocks/test1.txt", ios::in);

    int c;
    if(!fp)
    {
        cout<<"Cannot open file\n";
        return 1;
    }
    while(fp)
    {
        fp.get(c);
        cout << c;
    }
    //while ((c = getline(fp) != EOF))
      //  printf("%d\n", c);
}
#包括
#包括
使用名称空间std;
int main()
{
fstream-fp;
fp.open(“/home/Documents/C/Codeblocks/test1.txt”,ios::in);
INTC;
如果(!fp)
{

cout从文件中读取内容的真正好方法是使用流。通过使用流,您只需使用
>
运算符即可轻松读取以空格分隔的数字(例如换行符、空格等)。请阅读以下文章中的第一个答案,它非常适合您的问题:

你能做的就是

int c;
while (fp >> c)
{
    cout << c << " ";
}

从文件中读取内容的真正好方法是使用流。通过使用流,您只需使用
>
运算符即可轻松读取以空格分隔的数字(例如换行符、空格等)。请阅读以下文章中的第一个答案,它完全适合您的问题:

你能做的就是

int c;
while (fp >> c)
{
    cout << c << " ";
}

从文件中读取内容的真正好方法是使用流。通过使用流,您只需使用
>
运算符即可轻松读取以空格分隔的数字(例如换行符、空格等)。请阅读以下文章中的第一个答案,它完全适合您的问题:

你能做的就是

int c;
while (fp >> c)
{
    cout << c << " ";
}

从文件中读取内容的真正好方法是使用流。通过使用流,您只需使用
>
运算符即可轻松读取以空格分隔的数字(例如换行符、空格等)。请阅读以下文章中的第一个答案,它完全适合您的问题:

你能做的就是

int c;
while (fp >> c)
{
    cout << c << " ";
}
将代码更改为

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    int c;
    ifstream fp("/home/Documents/C/Codeblocks/test1.txt");
    if (!fp.is_open()) {
        cerr << "Cannot open file" << endl;
        return 1;
    }
    while (fp >> c)
        cout << c << endl;
    return 0;
}
#包括
#包括
使用名称空间std;
int main(){
INTC;
ifstream fp(“/home/Documents/C/Codeblocks/test1.txt”);
如果(!fp.is_open()){
cerr(c)
无法将代码更改为

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    int c;
    ifstream fp("/home/Documents/C/Codeblocks/test1.txt");
    if (!fp.is_open()) {
        cerr << "Cannot open file" << endl;
        return 1;
    }
    while (fp >> c)
        cout << c << endl;
    return 0;
}
#包括
#包括
使用名称空间std;
int main(){
INTC;
ifstream fp(“/home/Documents/C/Codeblocks/test1.txt”);
如果(!fp.is_open()){
cerr(c)
无法将代码更改为

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    int c;
    ifstream fp("/home/Documents/C/Codeblocks/test1.txt");
    if (!fp.is_open()) {
        cerr << "Cannot open file" << endl;
        return 1;
    }
    while (fp >> c)
        cout << c << endl;
    return 0;
}
#包括
#包括
使用名称空间std;
int main(){
INTC;
ifstream fp(“/home/Documents/C/Codeblocks/test1.txt”);
如果(!fp.is_open()){
cerr(c)
无法将代码更改为

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    int c;
    ifstream fp("/home/Documents/C/Codeblocks/test1.txt");
    if (!fp.is_open()) {
        cerr << "Cannot open file" << endl;
        return 1;
    }
    while (fp >> c)
        cout << c << endl;
    return 0;
}
#包括
#包括
使用名称空间std;
int main(){
INTC;
ifstream fp(“/home/Documents/C/Codeblocks/test1.txt”);
如果(!fp.is_open()){
cerr(c)

CUT<代码> GETLINE 也将在Linux上工作。它是标准C++。只需传递正确的参数。(提示:GETLINE不会按您预期的那样返回),至少在CODBULD上……代码> GETLINE < /C>也将在Linux上工作。它是标准C++。只需传递正确的参数。(提示:GETLINE不会按您预期的返回)。嗯,至少在CODBULD……代码> GETLINE < /> >也将在Linux上工作。它是标准C++。只需传递正确的参数。(提示:GETLINE不会按您预期的那样返回),至少在CODBULD上……代码> GETLINE < /C>也将在Linux上工作。它是标准C++。只需传递正确的参数就行了。(提示:getline并没有像您期望的那样返回)好吧,至少在代码块上没有。。。