fstream not working maybe??,

hi, i'm trying to read in a set of data from .txt file, but codeblocks just crashes!, is there something wrong with the code below??

=>Thanks!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
    vector<double> a;

    double num = 0.0;
    int cntr = 0;
    ifstream file("input.txt", ios::in);
    while(!file.eof()) {
        file>>num;
        a.push_back(num);
    }

    for(int i = 0; i< cntr; i++)
        cout<<a[i]<<endl;

    file.close();

}
Last edited on
Many things wrong with your code.

You are using some stuff and you did not include any header.

Add these to the top of your code:
1
2
3
#include <iostream> //For cout
#include <vector> //For using vector
#include <fstream> //For using ifstream 


And before int main() add using namespace std;
After file.close() add return 0; since the main() function is declared to return a type int.

Furthermore, where are you increasing cntr? It always remains 0 in the code. Add cntr++; in your while loop.

Your final code should look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

int main()
{
    vector<double> a;

    double num = 0.0;
    int cntr = 0;
    ifstream file("input.txt", ios::in);
    while(!file.eof()) {
        file>>num;
        a.push_back(num);
        cntr++;
    }

    for(int i = 0; i< cntr; i++)
        cout<<a[i]<<endl;

    file.close();
    
    return 0;
}


Also make sure your input file contains ONLY numbers and no letters.

Cheers!

-Stormboy
Last edited on
Thanks for the reply stormboy:

-thats exactly how my code looks like in codeblocks, just didnt copy everything SORRY!.. anyway i get the following errror msg from the terminal!
1
2
3
4
5
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc

This application has requested the Runtime to terminate it in an unusual w
Please contact the application's support team for more information. 


my input file look like this
1
2
3
4
1.0
2.0
3.0
4.0


what am i doing wrong! 0.o
Last edited on
+1 for stormboy!
He is correct!
Where is your input file? It should be in the same folder where your source code is.
in project folder, (same directory!)...
Can you try copying it to:
YOUR_PROJ_FOLDER\bin\Debug
i did!, it still gives me the same error!..

am i correct in saying the code is fine, there must be something wrong with my computer maybe?,

also tried it with DEVc++ and give me exactly the same error!
Can you download this and run the program in it. I've compiled the code and included the input file too. It runs fine in my computer. Tell me whether it works or not.
(Its a zip file. You need to extract it).
http://wikisend.com/download/361082/fileio.zip
Last edited on
It's not a good idea to use eof() in a while loop. For example, in your code, if the file has not been opened successfully, the fail() flag will be set, but eof() will never be set. The loop could continue until the vector is unable to allocate any more elements, then crash.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
    vector<double> a;

    double num = 0.0;
    
    ifstream file("input.txt");
    
    if (!file)
    {
        cout << "Error opening file" << endl;
        return 1;
    }
    
    while (file>>num)
        a.push_back(num);

    for (int i = 0; i< a.size(); i++)
        cout << a[i] << endl;
        
    return 0;
}
i used your input.txt and the code is working now.. i dont understand!!.. whats the convention of making a .txt input file for fstream! ? 0.o
Use notepad?
yeah, thats what i used, but it wasnt working!,

#anyway thanks a lot for the help, i really apreciate it!
Last edited on
You are welcome. BTW I used Notepad++ for making the text file. If you want it:
notepad-plus-plus.org
Last edited on
Thanks, i will check it out!
Topic archived. No new replies allowed.