reading in a list of integers.

This is a discussion on reading in a list of integers. within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I've been trying to figure out how to read in a list of integers from a text file ...

  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
    20,971
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,971
    Sorry, but I cannot duplicate your problem. Perhaps you should check to see if the file actually contains some valid input.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,525
    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, 05:55 PM
  3. Recursion Revisited again, and again!
    By clegs in forum C++ Programming
    Replies: 93
    Last Post: 12-08-2007, 07: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21