Thread: reading in a list of integers.

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    5

    reading in a list of integers.

    Hey guys, I've been trying to figure out how to read in a list of integers from a text file using visual c++ express 2010, and I'm completely stumped. This is the kind of thing that I'm trying now:

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main () {
      int x;
      ifstream myfile;
      myfile.open ("text.txt");
      if (!myfile) {
    	  cout << "unable to load file.\n";
      }
      while (myfile >> x) {
    	  cout << x << endl;
      }
      myfile.close();
      system("pause");
      return 0;
    }
    When I run this all I get is "press any key to continue..."
    So the file is correctly read, but I can't do anything with the data.


    text.txt contains integers on separate lines. Like this:

    12
    32
    14

    What am I doing wrong?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ben1220
    So the file is correctly read, but I can't do anything with the data.
    That appears to be false since you are printing the data.

    Perhaps you want to store all the data in a container such as a std::vector?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    5
    That's the point, I'm trying to print the data, but it just won't happen. I've tried several different ways from several websites but none of them work. I know the file is being opened properly because when I change the name, I get the error message "unable to load file."
    There must be something I'm missing...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Sorry, but I cannot duplicate your problem. Perhaps you should check to see if the file actually contains some valid input.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    I think the problem is with ifstream,

    Fix:


    #include <stdio.h>
    #include <windows.h>
    #include <iostream>
    #include <fstream>

    using namespace std;

    int main () {
    int x;
    ifstream ifs ( "text.txt" , ifstream::in );
    if (!ifs) {
    cout << "unable to load file.\n";
    }
    while (ifs >> x) {
    cout << x << endl;
    }
    ifs.close();
    system("pause");
    return 0;
    }
    I tried to help, but I dont know much about ifstream, I mix so much C with C++, in my codes I prefer to read files in C and use fread, much better to alocate the position of pointer.

    Here is one example that I did in my class:

    fread (&contato, sizeof(struct Agenda), 1, arquivo);

    while (!feof(arquivo)) {

    printf ("Nome: '%s'\n", contato.nome);
    printf ("Telefone: '%s'\n", contato.tel);
    printf ("E-mail: '%s'\n", contato.email);
    printf ("Aniversario: '%s'\n", contato.niver);
    puts ("");

    fread (&contato, sizeof(struct Agenda), 1, arquivo);

    }
    Sorry about the language is in portuguese.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    The only thing you did differently was use the constructor instead of the open method. ifstream is, AFAICT, being used correctly in both examples.

    If text.txt is successfully being opened, but there is no data being displayed, I can only think it's a different text.txt being opened, than the one that the OP wants to use.

    • is the current working directory the same as the one you wanted?
    • has text.txt previously been erroneously truncated by previous version of the program?


    If I'm on the right track, then adding C isn't going to help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Recursion Revisited again, and again!
    By clegs in forum C++ Programming
    Replies: 93
    Last Post: 12-08-2007, 08:02 PM
  4. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM