file input

Pages: 12
This was my attempt to read data in from a file. The file is stored in the same folder (directory) as the .cpp file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream> //ignore the surplus includes, my textbook hasn't taught...
#include <vector>   //...me when i need what yet
#include <cmath>
#include <fstream>
using namespace std;
int main() {
  ifstream infile;
  infile.open("words.txt");
  string data;
  infile >> data;
  infile.close();
  cout << data;
  return 0;
}


When I attempt to output the data I have just read in, it outputs "1.07151e+30" and I do not no why.

Help would be appreciated. Thanks!
Last edited on
hi, well firstly line 10 will only read in 1 element i.e the first thing in words.txt, if you want to read in more you need a loop for that,, secondly your code looks fine to me but you may try combining line 7 and 8 as:

 
ifstream infile("words.txt", ios::in);


hehehe and as for line 1, the "surplus" iostream, is called a header file and allows a user to display stuff to the console i.e cmd

check out the articles here in this website, i find they are more informative than most textbooks!

now to read in all that gunk in yout words.txt file use this
1
2
3
while(!infile.eof()){
infile>>words;
}


instead of line 10 which only reads in the first element!

This works with your words.txt in my pc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream> //ignore the surplus includes, my textbook hasn't taught...
#include <vector>   //...me when i need what yet
#include <cmath>
#include <fstream>
using namespace std;
int main()
{
    ifstream infile;
    infile.open("input.txt");
    string data;
    while(!infile.eof()) {
        infile >> data;
    }
    infile.close();
    cout << data;
    return 0;
}
Last edited on
what is .eof? I recognize it as a member function, but I don't know what it does.
just means keep reading from words.txt as long as there is still something in it!, its built into fstream, and stand for "end of file"
Last edited on
also i do not understand what you mean by the first "thing" will be read. There is no whitespace in the text file so where would the computer draw the line between "things".
hehee, i meant for cases where in there is a whitespace, i hadnt seen your words.txt when i typed that,, your initial program should reads in all the stuff in words.txt 0.o
Last edited on
Perhaps I am just compiling wrong? That is, where I am placing the files. I have the .out file in the same directory as the .txt file.
Ozwurld wrote:
while(!infile.eof()) {
No, do not loop on eof! Code that loops on eof like this is wrong. The correct way to loop input is to loop on the input operation itself:
1
2
3
4
while(infile >> data)
{
    //data is guaranteed to be valid
}
what would I stick inside the while loop?
closed account (Dy7SLyTq)
sometimes you dont. for example:
while(getline(some_stream, some_vector[i++]));
where some_vector is of type vector<string> or really any string container
@ L B

i have heard people say there is something wrong with that approach, could you please elaborate on what the flaw is:

=>Thanks!


Last edited on
The most likely culprit is that you don't have the text file in the working directory. One way you can determine which directory is the working directory without resorting to compiler specific methods is to create a file and note the location it was created in by inspecting directories after the fact:

1
2
3
4
5
6
#include <fstream>

int main()
{
    std::ofstream file("in_working_directory") ;
}


Your text file should be in the same place as the in_working_directory file created thusly.

That said, you should probably also check to make sure the file was opened correctly for input. You do not currently.

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
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <limits>

std::istream& read_word(std::istream& is, std::string& word)
{
    is.ignore(std::numeric_limits<std::streamsize>::max(), '"');    // word begins immediately after a '"'
    return std::getline(is, word, '"');                             // word ends immediately before a '"'
}

int main()
{
    const char* file_name = "test.txt";

    std::ifstream in(file_name);

    if (!in.is_open())
    {
        std::cerr << "Unable to open file \"" << file_name << "\"\n";
        return 0;
    }

    std::vector<std::string> words;

    {
        std::string working_word;
        while (read_word(in, working_word))
            words.push_back(working_word);
    }

    //for (auto& w : words)
    //    std::cout << w << '\n';

    std::cout << "Read in " << words.size() << " words.\n";
}




@DTSCode: no, I don't know where you read that, that is wrong.

@Ozwurld: with this wrong code there are some problems:
1
2
3
4
5
while(!infile.eof()) //condition is checked first
{
    infile >> data; //we try to read data second
    //at this point we don't know if data is valid or not
}
This issue is that even if the get pointer is at the end of the file, the EOF flag is not set until you try to read at the end of the file. This means you can read the last character and .eof() is still false! This leads to the last value to be processed being invalid.
closed account (Dy7SLyTq)
it is most certianly not wrong. eof is a character in the fileClosed account? Rage quite?

edit: my mistake. read this and realized i was wrong. ill have to consult my books again and see how old they are http://latedev.wordpress.com/2012/12/04/all-about-eof/
Last edited on
it is most certianly not wrong.


Instead of reiterating an incorrect assertion after you've been told it's wrong, perhaps checking your assumption would be in order?

http://stackoverflow.com/questions/12389518/representing-eof-in-c-code
closed account (Dy7SLyTq)
its not an assertion cire. i couldnt find anything in google to disprove me (until i changed the search conditions) and was basing my info on my books from dennis ritchie and brian kerningham and bajarne stroustrop. as i said though, i need to see how old they are
If you cast your input from int to char and try to compare it to the EOF macro, then you will have problems as the cast can cause natural data to compare equal to the EOF constant. Hence we don't read data this way ;)
DTSCode wrote:
its not an assertion cire.

I would let this slide if it wasn't another example of imprecision immediately following the previous one(s.) Please, use a dictionary before correcting people about the meaning of words.
Cire, your suggestion to create a file helped me solve my problem, thanks!
Pages: 12