Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 20th, 2008, 06:25 AM
noobles
Guest
 
Posts: n/a
Default Where do I put the file to parse it in Visual Studio '08

I'm trying to make a command line parser, but I'm not exactly sure
where to put my file.txt file so that this thing can read it. The most
logical place seemed to be the directory the project was in, but that
didn't work.


#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int main(){
ifstream file;
string fileName ="file.txt";
file.open(fileName.c_str());
if (!file){
cout << "Failed to open file" << endl;
return -1;
}
string newLine;
while(std::getline(file, newLine)) {
cout<<newLine<<endl;
}
return 1;
}
  #2  
Old March 20th, 2008, 06:35 AM
jason.cipriani@gmail.com
Guest
 
Posts: n/a
Default Re: Where do I put the file to parse it in Visual Studio '08

On Mar 20, 1:19 am, noobles <z3ph...@gmail.comwrote:
Quote:
I'm trying to make a command line parser, but I'm not exactly sure
where to put my file.txt file so that this thing can read it. The most
logical place seemed to be the directory the project was in, but that
didn't work.
If you do not specify an absolute pathname to the file, the filename
is relative to the current working directory when you start the
project. I believe that in VS, by default, this is the directory the
EXE ends up in -- which is your output directory (like "Release" or
"Debug"), not the project directory. You could try putting it there.

You could also go to your Project Properties -Configuration
Properties -Debugging and change "Working Directory" to the
directory that contains your file. VS will then change to that
directory before starting your program if you run it from the IDE.

Another option is to call SetCurrentDirectory() before opening the
file to set the current working directory to whatever.

Yet another option is to specify the absolute path to the file in your
program:

string fileName ="c:\\wherever\\file.txt";

In any case, if the filename is a relative path it must be in the
current working directory. Upon starting the program, VS sets the
current working directory to the output path by default (I think). So
pick any solution that puts the file in the current working directory.

That said, this question is not on topic for comp.lang.c++. In the
future you'll probably want to head to one of the microsoft.public.*
newsgroups instead. This newsgroup is for specific questions about the
syntax and semantics of the C++ language itself.

Jason
Quote:
>
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
>
int main(){
ifstream file;
string fileName ="file.txt";
file.open(fileName.c_str());
if (!file){
cout << "Failed to open file" << endl;
return -1;
}
string newLine;
while(std::getline(file, newLine)) {
cout<<newLine<<endl;
}
return 1;
>
}
  #3  
Old March 20th, 2008, 06:55 AM
noobles
Guest
 
Posts: n/a
Default Re: Where do I put the file to parse it in Visual Studio '08

Ya, I've tried most of those methods. I don't think this is a VS
issue, the code might have some problems or something.

On Mar 19, 10:33*pm, "jason.cipri...@gmail.com"
<jason.cipri...@gmail.comwrote:
Quote:
On Mar 20, 1:19 am, noobles <z3ph...@gmail.comwrote:
>
Quote:
I'm trying to make a command line parser, but I'm not exactly sure
where to put my file.txt file so that this thing can read it. The most
logical place seemed to be the directory the project was in, but that
didn't work.
>
If you do not specify an absolute pathname to the file, the filename
is relative to the current working directory when you start the
project. I believe that in VS, by default, this is the directory the
EXE ends up in -- which is your output directory (like "Release" or
"Debug"), not the project directory. You could try putting it there.
>
You could also go to your Project Properties -Configuration
Properties -Debugging and change "Working Directory" to the
directory that contains your file. VS will then change to that
directory before starting your program if you run it from the IDE.
>
Another option is to call SetCurrentDirectory() before opening the
file to set the current working directory to whatever.
>
Yet another option is to specify the absolute path to the file in your
program:
>
string fileName ="c:\\wherever\\file.txt";
>
In any case, if the filename is a relative path it must be in the
current working directory. Upon starting the program, VS sets the
current working directory to the output path by default (I think). So
pick any solution that puts the file in the current working directory.
>
That said, this question is not on topic for comp.lang.c++. In the
future you'll probably want to head to one of the microsoft.public.*
newsgroups instead. This newsgroup is for specific questions about the
syntax and semantics of the C++ language itself.
>
Jason
>
>
>
>
>
Quote:
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
>
Quote:
int main(){
* * * * ifstream file;
* * * * string fileName ="file.txt";
* * * * file.open(fileName.c_str());
* * * * if (!file){
* * * * * * * * cout << "Failed to open file" << endl;
* * * * * * * * return -1;
* * * * }
* * * * * * * * string newLine;
* * * * * * * * while(std::getline(file, newLine)) {
* * * * * * * * * * * * cout<<newLine<<endl;
* * * * * * * * }
return 1;
>
Quote:
}- Hide quoted text -
>
- Show quoted text -
  #4  
Old March 20th, 2008, 07:55 AM
jason.cipriani@gmail.com
Guest
 
Posts: n/a
Default Re: Where do I put the file to parse it in Visual Studio '08

On Mar 20, 1:54 am, noobles <z3ph...@gmail.comwrote:
Quote:
Ya, I've tried most of those methods. I don't think this is a VS
issue, the code might have some problems or something.
Maybe. Your code looks relatively simple, though. I don't know what
else to tell you. Perhaps as a test, try:

#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;

int main () {
FILE *file;
string fileName ="file.txt";
file = fopen(fileName.c_str(), "rt");
if (!file) {
perror("Could not open file");
return -1;
}
// ...
}

At least with fopen() you can get a reasonable error message (if it
says "file not found", then you know the problem is that it's not in
the path you think it is in).

Incidently, if anybody knows how to get a reliable error message from
an ifstream after a failed operation, I'd love to know. It's always
kind of bothered me.

Jason

Quote:
>
On Mar 19, 10:33 pm, "jason.cipri...@gmail.com"
>
<jason.cipri...@gmail.comwrote:
Quote:
On Mar 20, 1:19 am, noobles <z3ph...@gmail.comwrote:
>
Quote:
Quote:
I'm trying to make a command line parser, but I'm not exactly sure
where to put my file.txt file so that this thing can read it. The most
logical place seemed to be the directory the project was in, but that
didn't work.
>
Quote:
If you do not specify an absolute pathname to the file, the filename
is relative to the current working directory when you start the
project. I believe that in VS, by default, this is the directory the
EXE ends up in -- which is your output directory (like "Release" or
"Debug"), not the project directory. You could try putting it there.
>
Quote:
You could also go to your Project Properties -Configuration
Properties -Debugging and change "Working Directory" to the
directory that contains your file. VS will then change to that
directory before starting your program if you run it from the IDE.
>
Quote:
Another option is to call SetCurrentDirectory() before opening the
file to set the current working directory to whatever.
>
Quote:
Yet another option is to specify the absolute path to the file in your
program:
>
Quote:
string fileName ="c:\\wherever\\file.txt";
>
Quote:
In any case, if the filename is a relative path it must be in the
current working directory. Upon starting the program, VS sets the
current working directory to the output path by default (I think). So
pick any solution that puts the file in the current working directory.
>
Quote:
That said, this question is not on topic for comp.lang.c++. In the
future you'll probably want to head to one of the microsoft.public.*
newsgroups instead. This newsgroup is for specific questions about the
syntax and semantics of the C++ language itself.
>
Quote:
Jason
>
Quote:
Quote:
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
>
Quote:
Quote:
int main(){
ifstream file;
string fileName ="file.txt";
file.open(fileName.c_str());
if (!file){
cout << "Failed to open file" << endl;
return -1;
}
string newLine;
while(std::getline(file, newLine)) {
cout<<newLine<<endl;
}
return 1;
>
Quote:
Quote:
}- Hide quoted text -
>
Quote:
- Show quoted text -
  #5  
Old March 20th, 2008, 07:55 AM
jason.cipriani@gmail.com
Guest
 
Posts: n/a
Default Re: Where do I put the file to parse it in Visual Studio '08

On Mar 20, 2:49 am, "jason.cipri...@gmail.com"
<jason.cipri...@gmail.comwrote:
Quote:
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;
Whoops, you don't need the string.h.
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles