tags:

views:

93

answers:

4

I am using Windows 7 and I have created on the desktop a file named test.txt. How can I access this file using C++? For example, consider following code:

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

int main(int argc, char *argv[])
{

  fstream inout("test.txt", ios::in | ios::out | ios::binary);

  if(!inout) {
    cout << "Cannot open input file.\n";
    return 1;
  }

  long e, i, j;
  char c1, c2;
  e = 5;

  for(i=0, j=e; i<j; i++, j--) {
    inout.seekg(i, ios::beg);
    inout.get(c1);
    inout.seekg(j, ios::beg);
    inout.get(c2);

    inout.seekp(i, ios::beg);
    inout.put(c2);
    inout.seekp(j, ios::beg);
    inout.put(c1);
  }

  inout.close();
  return 0;
}

In the fragment fstream inout("test.txt", ios::in | ios::out | ios::binary), what should I change to access my test.txt on desktop?

+1  A: 

Easiest way is to use ifstream, that is ifstream inputfile("\path\to\input\file); What path it is is easiest seen using your explorer or whatever it is called these days.

Esben Mose Hansen
+1: I still call it explorer.
JoshD
+1  A: 

You need to provide the full path to the file. Determine what the absolute path is for the file and use that in the first argument:

fstream inout("c:\\some\\whole\\path\\to\\docmument\\test.txt", ios::in | ios::out | ios::binary);
JoshD
Better make those "\\"s
Dakota Hawkins
Thanks :) . . . ... .. . .. .
JoshD
+1  A: 

I would try to provide the abosolute path to test.text, which can be something like (I'm not sure):

C:\WINDOWS\Desktop\test.txt
ArunSaha
+1, though now adays it's much longer...
JoshD
In Win7 it's "C:\Users\<username>\Desktop"
Travis Christian
Yes, "Users" was a much better choice than "Documents and Settings".
dreamlax
But since it can vary ("Documents and Settings" vs. "Users") then either choice is bad. It's also customizable and could be neither of those, and isn't even required to be on `C:`
Stephen P
Not to mention translated versions. Oh, and since it contains a username: don't assume the path is ASCII.
MSalters
+7  A: 

Are you asking how to access that file location from your program? If so, you need to put the file and the executable in the same directory, or include a full path to the file's location:

"%USERPROFILE%\\Desktop\\test.txt"

Unless there is a full file path starting from a drive letter (the variable %USERPROFILE% evaluates to C:\Users\ {your username} in your case) the executable will look for the file relative to its own location. Since your string contains only the file name, it will look in its own directory.

Travis Christian
Actually `%USERPROFILE%` evaluates to `%HOMEDRIVE%%HOMEPATH%` and `%HOMEPATH%` expands based on the particular OS version. On XP it's `\Documents and Settings\%USERNAME%[.%USERDOMAIN%]`. But anyway.. *big* +1 for not just putting `C:\...` like most of the other answers.
Stephen P
OP mentioned Windows 7 so I assumed it was the typical setup. Just trying to explain what the variable meant without going into too much detail.
Travis Christian