Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

File path in c++


  • Please log in to reply
5 replies to this topic

#1 Frith

Frith

  • Members
  • 7 posts
  • OFFLINE
  •  
  • Local time:08:50 PM

Posted 19 September 2010 - 06:27 PM

I'm learning c++ and I'm trying to open a file and write something in it but I can't write a good file path.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
fstream (myfile);

myfile.open ("C:\\users\\[user]\\Desktop\\[filenam .txt]");

if (myfile.is_open())
{
myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
}
else cout << "Unable to open file";

return 0;
}

BC AdBot (Login to Remove)

 


#2 Frith

Frith
  • Topic Starter

  • Members
  • 7 posts
  • OFFLINE
  •  
  • Local time:08:50 PM

Posted 19 September 2010 - 07:00 PM

Found the solution... I read on a forum that the File path was supposed to be in double slashes but it doesn't


#include <iostream>
#include <fstream>

using namespace std;

int main()
{
fstream (myfile);

myfile.open ("C:/Users/.../Desktop/IO.txt");

if (myfile.is_open())
{
myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
}
else cout << "Unable to open file";

return 0;
}

#3 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:07:50 PM

Posted 21 September 2010 - 05:04 PM

It may help you in the future if you take a moment and notice that you have demonstrated two styles of urls. A back-slash like in your first example must be preceded by another back-slash to let the compiler know that you intend to keep the back-slash as part of the string. The back-slash is a special character and must be handled.

In your second example, you are using a forward slash to indicate a separation between directories. So in the first case, yes you do need to use double back-slashes in order for the path name to resolve properly.

#4 prog_digy

prog_digy

  • Members
  • 1 posts
  • OFFLINE
  •  
  • Local time:07:20 AM

Posted 17 September 2013 - 06:24 AM

is this a valid file name?

 

fstream file("C:\\Documents and Settings\\[username]\\My Documents\\IO.txt",ios::in||ios::out);

 

The compiler returned a positive result with fstream file("C:\\IO.txt",ios::in||ios::out) but not with the above line..

is there something to add b\w the spaces??



#5 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:07:50 PM

Posted 17 September 2013 - 07:58 AM

Double slash does not work in a path name like that. Use single slashes.

C:\Documents and Settings\[username]\My Documents\IO.txt



#6 Romeo29

Romeo29

    Learning To Bleep


  • Members
  • 3,194 posts
  • OFFLINE
  •  
  • Gender:Not Telling
  • Location:127.0.0.1
  • Local time:08:50 PM

Posted 27 September 2013 - 01:08 AM

Just curious. Are you replacing [username] with your Windows username or not?






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users