-
September 6th, 2009, 11:16 PM
#1
[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.
-
September 7th, 2009, 05:41 AM
#2
Re: Simply reading txt file - oddly not working
Try this
while(in>>str)
{
//do something with str
}
hi,,,
-
September 7th, 2009, 08:06 AM
#3
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.
-
September 7th, 2009, 01:30 PM
#4
Re: Simply reading txt file - oddly not working
Originally Posted by Khiem
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.
-
September 7th, 2009, 01:32 PM
#5
Re: Simply reading txt file - oddly not working
Originally Posted by Philip Nicoletti
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
-
September 7th, 2009, 01:44 PM
#6
Re: Simply reading txt file - oddly not working
Wrap an if statement around the getline() call to test whether or not it succeeds.
-
September 7th, 2009, 02:15 PM
#7
Re: Simply reading txt file - oddly not working
Originally Posted by Lindley
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
-
September 7th, 2009, 02:35 PM
#8
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
-
September 7th, 2009, 02:46 PM
#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");
-
September 7th, 2009, 03:05 PM
#10
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.
-
September 7th, 2009, 03:09 PM
#11
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.
-
September 7th, 2009, 03:20 PM
#12
Re: Simply reading txt file - oddly not working
Originally Posted by Lindley
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|