[RESOLVED] Simply reading txt file - oddly not working
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: [RESOLVED] Simply reading txt file - oddly not working

  1. #1
    Join Date
    Sep 2009
    Posts
    9

    [RESOLVED] Simply reading txt file - oddly not working

    I've tried reading a file several different ways. I get nothing every time. The code seems exactly right.

    Read a file >> print the text.
    Simple.
    I've written similar code.... what am I missing guys?

    (I have the text file in the solution dir, and the project dir - pretty sure i just need it in the project dir? but i wasn't sure - c++ AND VS n00b )

    I've also tried using a whole pathname to read the text file. I get nothing.

    Here's my brilliant code :

    // stdafx.h : include file for standard system include files,
    // or project specific include files that are used frequently, but
    // are changed infrequently
    //

    #pragma once

    #include "targetver.h"

    #include <stdio.h>
    #include <tchar.h>

    #include <iostream>
    #include <fstream>
    #include <string>

    --------

    // test.cpp : Defines the entry point for the console application.
    //

    #include "stdafx.h"

    using namespace std;


    int _tmain(int argc, _TCHAR* argv[])
    {
    string str;
    ifstream in("test.txt", ios::in);
    in.open("test.txt");
    getline(in, str);

    cout << str << endl;

    in.close();

    return 0;
    }



    jacobi
    Last edited by jacobis; September 7th, 2009 at 10:27 PM.

  2. #2
    Join Date
    Aug 2008
    Posts
    112

    Re: Simply reading txt file - oddly not working

    Try this
    while(in>>str)
    {
    //do something with str
    }
    hi,,,

  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,565

    Re: Simply reading txt file - oddly not working

    Code:
    ifstream in("test.txt", ios::in);
    The above line opens the file test.txt for reading

    Code:
    in.open("test.txt");
    That line attempts to open the file test.txt for reading. Since it
    is already open, the statement fails. This sets the streams state
    to "bad", so any attempt to read from the stream fails.

    Just remove the open line.

  4. #4
    Join Date
    Sep 2009
    Posts
    9

    Unhappy Re: Simply reading txt file - oddly not working

    Quote Originally Posted by Khiem View Post
    Try this
    while(in>>str)
    {
    //do something with str
    }
    This doesn't change the fact that it won't read my text file.

    it should at least give the first word before the whitespace delimiter. But it doesn't.

    If it's reading the file, it's not outputting what I stored in str.

  5. #5
    Join Date
    Sep 2009
    Posts
    9

    Re: Simply reading txt file - oddly not working

    Quote Originally Posted by Philip Nicoletti View Post
    Code:
    ifstream in("test.txt", ios::in);
    The above line opens the file test.txt for reading

    Code:
    in.open("test.txt");
    That line attempts to open the file test.txt for reading. Since it
    is already open, the statement fails. This sets the streams state
    to "bad", so any attempt to read from the stream fails.

    Just remove the open line.
    I have removed in.open();

    Still no output.... i think it's an error with my getline(); statement

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Simply reading txt file - oddly not working

    Wrap an if statement around the getline() call to test whether or not it succeeds.

  7. #7
    Join Date
    Sep 2009
    Posts
    9

    Re: Simply reading txt file - oddly not working

    Quote Originally Posted by Lindley View Post
    Wrap an if statement around the getline() call to test whether or not it succeeds.
    if( !getline(in, str) )
    {
    cout<< "error in getline()";
    }
    else{
    cout << str << endl;
    }
    in.close();

    Outputs "error in getline()"

    hmm why oh why

  8. #8
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: Simply reading txt file - oddly not working

    try
    Code:
    ifstream in("test.txt", ios::in);
    if ( ! in ) {
        cout << "file 'test.txt' not found." << endl;
    }
    Kurt

  9. #9
    Join Date
    Sep 2009
    Posts
    9

    Re: Simply reading txt file - oddly not working

    ifstream in("test.txt", ifstream::in);
    if(in.is_open())
    {
    cout << "iiiiit's open :-D ";
    }
    else
    cout<<"iiiiit's not open";


    Output: it's not open.

    So the problem is, the file isn't opening.

    test.txt is definitely in the directory

    I've even tried ifstream in("c:\\temp\\test.txt"); <--- file is in temp also.
    and i've tried:

    ifstream in;
    in.open("c:\\temp\\test.txt");

  10. #10
    Join Date
    Sep 2009
    Posts
    9

    Re: Simply reading txt file - oddly not working

    Could it be a visual studio problem?

    I can't understand why something so easy is giving me so much trouble.

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Simply reading txt file - oddly not working

    Show us your local directory structure related to the project.

    Also, check to make sure that the project's "Working Directory" field isn't something unexpected.

    And try dropping the second parameter of the ifstream constructor. I don't know that's the problem, but the default has always worked for me.

  12. #12
    Join Date
    Sep 2009
    Posts
    9

    Re: Simply reading txt file - oddly not working

    Quote Originally Posted by Lindley View Post
    Show us your local directory structure related to the project.
    Not sure about the local directory structure.

    Also, check to make sure that the project's "Working Directory" field isn't something unexpected.
    Working directory was blank, i changed it to the project folder. didn't help.

    And try dropping the second parameter of the ifstream constructor. I don't know that's the problem, but the default has always worked for me.
    test.txt still not open.


    Thanks for all your input!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center