473,569 Members | 2,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(fileN ame.c_str());
if (!file){
cout << "Failed to open file" << endl;
return -1;
}
string newLine;
while(std::getl ine(file, newLine)) {
cout<<newLine<< endl;
}
return 1;
}
Mar 20 '08 #1
4 1934
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 SetCurrentDirec tory() 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.publi c.*
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(fileN ame.c_str());
if (!file){
cout << "Failed to open file" << endl;
return -1;
}
string newLine;
while(std::getl ine(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 SetCurrentDirec tory() 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.publi c.*
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(fileN ame.c_str());
* * * * if (!file){
* * * * * * * * cout << "Failed to open file" << endl;
* * * * * * * * return -1;
* * * * }
* * * * * * * * string newLine;
* * * * * * * * while(std::getl ine(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 SetCurrentDirec tory() 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.publi c.*
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(fileN ame.c_str());
if (!file){
cout << "Failed to open file" << endl;
return -1;
}
string newLine;
while(std::getl ine(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
1606
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 again closes. I can close the other files without any problems. So what I wanted to do is find where Visual Studio saves which files you have open...
2
2726
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 of the labs at the university we will simulate a space shuttle so there is a space.hpp and a space.obj. So visual studio must be able to locate and...
3
1969
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 Visual Sorce Safe on a seprate machine. For a new person who is trying to setup this solution file on his / her local machine follows the following...
0
1116
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 related file (like a usercontrol) you receive the message: "The class file "WebForm1.aspx.cs" specified as the codebehind for
1
6124
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 and ultimately generate an XML file based on that schema? Background: I've been given an XSD file by a 3rd party. I need to generate XML files...
8
1844
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 I can find step by step information, I would be greatly obliged.
2
2873
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 configuration is saved. any information is appreciated... thanks
0
864
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 Win 2003 Server R2 Standard. I was wondering which operating system handles VS better. Also if somone could link me to a good article or two that backs...
3
10494
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 closes unexpectedly and without any error message being displayed, or error messages written to the system logs. Visual Studio closes when I attempt...
0
7609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6278
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.