如何从文件中读取

时间:2013-06-20 21:48:48

标签: c++

* 对于遇到此问题的人来说,这是我的命令问题。确保编译和运行程序的命令是正确的!

我正在观看youtube上的教程。

我的操作和操作完全一样,我得到了不同的输出。

我是c ++的新手,我正在使用Xcode来运行代码。

这里是代码:(再次,它不是我的,但来自youtube教程视频)

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int input[100];
    int x = 0;
    int fail = 0;
    int pass = 0;

    fstream textfile;
    textfile.open("exam.txt");

    while(! textfile.eof()) {
        textfile >> input[x];
        cout << input[x] << endl;
        if(input[x] < 50) {
            fail++;
        }
        else {
            pass++;
        }
        x++;
    }
    textfile.close();
    cout << fail << "students out of " << (fail + pass) << "did not pass the exam." << endl;

    return 0;}

它应该打印成绩。

但这是我在Xcode上运行时得到的结果。

0
0
0
0
0
0
0
0
1606415288
32767
1606415720
32767
1606415720
32767
2003953588
1294056514
104
1
1606415312
32767
25240
1
1606415288
32767
1606415312
32767
25248
1
1606415288
32767
1606415312
32767
1606415720
32767
25264
1
1606415304
32767
1606415720
32767
1606415720
32767
6144
1
1910210376
32767
1606416728
32767
0
0
0
0
0
0
0
0
0
0
1606416544
32767
2003953588
1294056514
1606416544
32767
-1988827167
32767
0
0
1
0
1606416904
32767
0
0

这只是输出的一部分。它不会终止。

另外,当我尝试在linux上运行它(sshed into school linux machine)时,

我收到以下错误:

    @@?!@8  @ @@@@@?88@8@@@?? ??`?`?? ``?TT@T@DDP?tdPP@P@DDQ?tR?td??`?`  /lib64/ld-linux-x86-64.so.2GNUGNU?Yd?`˫sP???*??"b!!??  (E?L?
                                             CyIk?  ^Q#y??K@??'?[?
@3P
@ `??
@libstdc++.so.6__gmon_start___Jv_RegisterClasses_ZNSt8ios_base4InitD1Ev_ZNSt13basic_fstreamIcSt11char_traitsIcEEC1Ev_ZNSt13basic_fstreamIcSt11char_traitsIcEE5closeEv_ZNSirsERi_ZNSt13basic_fstreamIcSt11char_traitsIcEE4openEPKcSt13_Ios_Openmode__gxx_personality_v0_ZSt4cout_ZNSolsEi_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc_ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6__ZNSt13basic_fstreamIcSt11char_traitsIcEED1Ev_ZNSolsEPFRSoS_E_ZNKSt9basic_iosIcSt11char_traitsIcEE3eofEv_ZNSt8ios_base4InitC1Evlibgcc_s.so.1_Unwind_Resumelibc.so.6__stack_chk_fail__cxa_atexit__libc_start_mainGCC_3.0GLIBC_2.4GLIBC_2.2.5CXXABI_1.3GLIBCXX_3.4 P&y
xui     ?ӯk?t)??`? `  ` ` `  `( `0 8 `  @ `                            p60ii
H `
   P `
` `h `p `x `? `H??c????H???5? ?%? @?%? h??????%? h??????%? h??????%? h?????%? h?????%? h?????%? h?????%? h?p????%? ?`????%? h   ?P????%? h
?@????%? h
          ?0????%? h
?????%z h?????%r h??????%j h?????1?I??^H??H???PTI???@H??0@H??
                                                             @????????H?H?? H??t??H?Ð????????????UH??SH??=0 uK?`H?* H???`H??H??H9?s$fDH??H? ???`H?? H9?r??? H?[]?fff.?H?=  UH??t?H??]`??]Ð?UH??SH???dH?%(H?E?1?Dž????Dž????Dž????H??????H???T???????H???????@H???????|H??0?????????Hc?H??H?H??????H??H???$?????????H????0????ƿ? `?Y?????
@H???
     ?????????H????0?????1  ???????????????????H??????H???????????d???H??????H???R???????????????????????ƿ? `??????%@H????????H???????6@H?????????
@H???`????H??????H?????????H?U?dH3%(t$?H??H??????H???????H??H???L????????H???[]?UH??H???}??u??}?u*?}???u!??!`?6????P
@?? `??!`H?????????UH?????????]?UH??}??u??U??E? ?]Ð???H?l$?L?d$?H?-? L?%? L?l$?L?t$?L?|$?H?\$?H??8L)?A??I??H??I???k???H??t1?@L??L??D??A??H??H9?u?H?\H?l$L?d$L?l$ L?t$(L?|$0H??8???Ð?????????????UH??SH?H H???t??`DH???H?H???u?H?[]Ð?H??_???H??exam.txtstudents out of did not pass the exam.D????`?????s?????????????????0p???XzRx
                              $H??? FJ
O                                     ??;*3$"D8???A?C

我使用了这个命令:g ++ main.cpp -o output.out 我的命令有问题吗?

非常感谢!!!

2 个答案:

答案 0 :(得分:3)

你在做一些蠢事。如前所述,请勿测试eof。您实际上需要测试您的输入是否成功和/或流是否良好。当你在阅读流时,你可以拥有一个全能的东西:

while( infile )
{
    //...
}

我还提到你需要检查是否已读取该值。可能是因为输入流不包含看起来像整数的东西,或者可能发生了一些其他错误,所以无法读取整数。由于您一次只读取一个整数,因此您可以自行测试该操作。如果流开始不好(例如文件无法打开),它将立即失败:

while( infile >> input[x] )
{
    //...
}

如果每次读取多个值,我通常不会使用上述方法,因为循环条件变得混乱。这是另一种选择:

while( infile )
{
    if( !(infile >> input[x]) ) {
        cout << "Failed to read value " << x+1 << endl;
        break;
    }

    //...
}

让我们一起来,因为当你想知道发生了什么时,它会更有用。接下来就是你有一个固定大小的数组,有100个值。如果你读了太多的值,你就会溢出。所以你需要确保不会发生这种情况:

while( infile && x < 100 )
{
    if( !(infile >> input[x]) ) {
        cout << "Failed to read value " << x+1 << endl;
        break;
    }

    cout << input[x] << endl;

    if( input[x] < 50 )  {
        fail++;
    } else {
        pass++;
    }

    x++;
}

现在,在我看来你甚至没有使用阵列,为什么要保留它呢?你可以读入一个临时值:

    int grade;
    if( !(infile >> grade) ) break;
    // ...

但是如果你想保留阵列并且不知道你可能最终读取了多少等级,请使用vector<int>而不是int数组。我将用更简单的循环风格来展示它:

std::vector<int> grades;
int grade;

while( infile >> grade )
{
    grades.push_back(grade);
    //...
}

请注意我是如何使用更多描述性变量名称的。像input这样的东西并没有告诉你它包含什么。这是你从没有经验的程序员那里获得的变量名称,你应该立即抵制你的YouTube视频。

我建议的最后一件事是使用ifstream而不是fstream,因为默认情况下您不需要以读/写模式打开文件。您可以向fstream::open提供标记,但为什么这样做可以像这样构建ifstream

ifstream infile("exam.txt");

您也不需要显式关闭它,因为在infile的析构函数被调用时会发生这种情况。

答案 1 :(得分:-3)

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

void main ()
{
        string STRING;
    ifstream infile;
    infile.open ("names.txt");
        while(!infile.eof) // To get you all the lines.
        {
            getline(infile,STRING); // Saves the line in STRING.
            cout<<STRING; // Prints our STRING.
        }
    infile.close();
    system ("pause");
}

我希望,这段代码可以帮到你。