473,385 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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;
}
Mar 20 '08 #1
4 1927
On Mar 20, 1:19 am, noobles <z3ph...@gmail.comwrote:
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
>
#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;

}
Mar 20 '08 #2
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:
On Mar 20, 1:19 am, noobles <z3ph...@gmail.comwrote:
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


#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;
}- Hide quoted text -

- Show quoted text -
Mar 20 '08 #3
On Mar 20, 1:54 am, noobles <z3ph...@gmail.comwrote:
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

>
On Mar 19, 10:33 pm, "jason.cipri...@gmail.com"

<jason.cipri...@gmail.comwrote:
On Mar 20, 1:19 am, noobles <z3ph...@gmail.comwrote:
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
#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;
}- Hide quoted text -
- Show quoted text -
Mar 20 '08 #4
On Mar 20, 2:49 am, "jason.cipri...@gmail.com"
<jason.cipri...@gmail.comwrote:
#include <stdio.h>
#include <string.h>
#include <string>
using namespace std;
Whoops, you don't need the string.h.
Mar 20 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Veerle | last post by:
Hi, I have 5 files open in Visual Studio .NET. When I try to close the second file, Visual Studio closes too. When I reopen Visual Studio and then try to close the second file, Visual studio...
2
by: Tony Johansson | last post by:
Hello experts!! I'm studying at the university at Karlstad in Sweden. At this university we use visual studio.net when we do mandatory labs. At home I use visual studio version 6.0. In one...
3
by: Paras Sharma | last post by:
Hi all, We are facing this big problem. Scenario is as follows. We have one single solution (say EIS) under which there are 25 projects. All the files are saved at a central location under...
0
by: google | last post by:
Hi, after fighting with an issue in Visual Studio.NET 2003 in aspx, I've finally figured out what it is and maybe you might find it usefull. Symptoms: When you try to open a .aspx or...
1
by: donnie.hale | last post by:
Question: What's the "canonical" way to import an existing XSD schema file into VS2005 in such a way that I can use standard C# object / property techniques to create content of that schema type...
8
by: Kuldeep | last post by:
Framework: Visual Studio 2005 Technology: ASP.NET 2.0 Language: C#.NET 2.0 Hi All, Could any one of you please let me know how to use NDoc in Visual Studio 2005. If there is a source where...
2
by: CMOS | last post by:
hi, im adding lot of directory path's to project directories in visual studio 2003. i want to save this setup for future use without going through this process again. im wondering where this...
0
by: dudebodacious | last post by:
I want to start developing with ASP.Net, and I have two computers on which I could work. One is my server and the other is my personal computer. My PC is running Win xp professional, and my server...
3
by: Johnson | last post by:
I'm not sure if this is an IIS 5.1 issue or ASP.NET issue, or Visual Studio 2008 issue -- thus posting to 3 groups. Please don't be offended. The problem I'm encountering is that Visual Studio...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.