The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Opening user-specified .txt file
Discuss Opening user-specified .txt file in the C Programming forum on Dev Shed. Opening user-specified .txt file C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

October 21st, 2005, 04:37 PM
|
|
Contributing User
|
|
Join Date: Apr 2004
Posts: 124
Time spent in forums: 11 h 3 m 34 sec
Reputation Power: 0
|
|
|
Opening user-specified .txt file
Is there a standard function in C++ that can open a text file for processing where that text file is determined by user input? Thanks in advance
|

October 21st, 2005, 04:54 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
Yes. Your standard file I/O functions do this.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
|

October 21st, 2005, 04:54 PM
|
|
Contributing User
|
|
Join Date: Apr 2004
Posts: 124
Time spent in forums: 11 h 3 m 34 sec
Reputation Power: 0
|
|
|
Which one?
|

October 21st, 2005, 04:57 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
The fstream object's open() method for C++ and fopen() for C. Am I missing something here?
|

October 21st, 2005, 05:20 PM
|
|
Contributing User
|
|
Join Date: Apr 2004
Posts: 124
Time spent in forums: 11 h 3 m 34 sec
Reputation Power: 0
|
|
What I mean is it how can I tell if that file exists or not, like if I needed to output
Code:
Enter file to process: "blah.txt"
ERROR: File "blah.txt" does not exist.
|

October 21st, 2005, 05:42 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
Quick N' Dirty way would be to check the return value of fopen() and make sure that it is not NULL. For C++, you could check fstream.good() after fstream.open(). Of course, failure doesn't necessarily mean that the file doesn't exist. You may not have permissions to open the file.
The alternative would be to use stat() or FindFirst() to check if the file exists.
|

October 21st, 2005, 11:38 PM
|
 |
Contributing User
|
|
|
|
Also,
Code:
#include <iostream>
#include <fstream>
#include <cstdio>
int main()
{
static const char filename[] = "blah.txt";
std::ifstream file(filename);
if ( file )
{
std::cout << "Opened " << filename << '\n';
}
else
{
perror(filename);
}
return 0;
}
/* my output
blah.txt: No such file or directory
*/
__________________
Any advertisement triggered by IntelliTxt in this post is not endorsed by the author of this post.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|