Use string as filepath for reading file on a different computer

I have my application to copy a file from a different persons computer. It just uses the normal network directory of //<ComputerName>. It gets the person to input the name of the computer they want to connect to. I effectively have;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
string AnotherPartOfDirectory;
string OnePartOfDirectory;
string directory;
string WhatsInFile;
ifstream AnInputFstream;

int main() {
   AndAgain:
   getline(cin,AnotherPartOfDirectory);
   directory = OnePartOfDirectory+AnotherPartOfDirectory;
   AnInputFstream.open(directory.c_str())
   getline(AnInputFstream,WhatsInFile)
   AnInputFstream.close();
   cout << WhatsInFile << endl;
   goto AndAgain;
}


I've only included <iostream>.

This works the first time I try it; but, when, without closing the application, I try it again (using a goto function to repeat).
I've tried it changing the 'directory.c_str()' part to an absolute directory on my computer going through "c:/" it works fine every time. But when I put my own computer name in to test it and keep the 'directory.c_str()' part, it only works the first time. After that it doesn't put a value into WhatsInFile, and displays nothing. And if I put in WhatsInFile = "" right before the second getline, it doesn't work after the first time even if I target my own computer's c:/ drive as part of the directory.
This can't be your entire code:

- You don't seem to define what OnePartOfDirectory contains.

- You seem to use fstream with out declaring it.

- You use namespace std without declaring it.

- You are missing semicolons in a few places.

We usually ask you to post your entire code as long as it isn't an absurd length. If for some reason you can't we'll still try to help you but we need more specifics.
closed account (4Gb4jE8b)
or we say go to pastebin.com to post entire code :)
What I've done here is copy out the specific code that isn't working in my file (which is over 600 lines) and tried to figure out why in another, test application. I'm not sure which things I need to include from my main application, so I just brought all of it over into it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <windows.h>
#include <stdio.h>
using namespace std;

string OpponentID;
string DefaultPathToThem;
string TheirNameFile;
string TheirName;
ifstream GetInfoFromThem;

int main() {
     ReenterComputerName:
     getline (cin, OpponentID);
     
     TheirNameFile = "\\\\"+OpponentID+"\\Users\\Public\\YourName.txt";
     GetInfoFromThem.open(TheirNameFile.c_str());
     getline(GetInfoFromThem,TheirName);
     GetInfoFromThem.close();
     cout << "Opponent:        " << TheirName << endl << endl;
     
     if (TheirName != "") {
                   cout << "Success" << endl;
     } else {
                 cout << endl << endl << "Oh dear. Check the spelling of the computer name." ;                 
     }
    goto ReenterComputerName
    return 0;
}


It's modified a little bit, however; this code is usually inside a function that is called from main, I've changed the cout a little and the goto name is different. (I'm shy about my code... don't ask why)
I'm testing it by putting in my own computer name, then a bogus computer name, then changing the "YourName.txt" file's contents, then putting in my own computer name.
That is the correct directory for windows 7. I know it's different for Windows Vista and XP, and I guess I'll figure out compatibility later. Although, it's an application for use between friends on the same network, and we all have Windows 7.
While I'm at that, how would I check to make sure the computer name exists rather than letting it fail to find the computer name? (It lags up for about 15 seconds when I put the bogus computer name in)
Last edited on
closed account (4Gb4jE8b)
you're using goto in the wrong way, and it seems a for loop or a do-while loop would make it more accessible and easier to do IF you have limiting factor (a point where you want it to stop).

http://www.cplusplus.com/forum/beginner/2287/

I suggest a do-while loop as you can use while theirname != "" as your limiting factor. Hopefully that solves your problem, but if it doesn't can you post a little more explicitly what is going on up to this point in the program?

You know after looking at your code for a bit, you could use stringstreams very effectively here

stringstream theirnamefile << "\\\\" << opponentid << "\\users..."

then append .str() to any reference to theirnamefile and you can gaurentee it will work a little easier (imo at least)
Last edited on
No, sorry, the goto is working for me fine. I know how to do a while loop that will achieve the same as goto, but I wonder why goto is such bad practise? (I learnt I could do that with goto and thought to myself; "That's awesome". But apparently it's not?)

This code is at the start of the program.
And what exactly happens is that the first time I put anything in, it will work how I expect. But then, the second time I do, it doesn't work how I expect. For example; if I put in my computer name the first time, it will correctly load in the information, then if I put in my computer name again, it won't. (I change the data of the .txt file between goes to check this). Or if I put in a bogus computer name (like I enter: "Noone" into it) it will come back saying there is no data, then if I put my computer name in, it still comes back saying there is no data.
I should also point out at this point, I have made a completely new file from scratch that can read the data properly, but I haven't made it so that you can enter the computer name in that one. (Part of my process to figure out what the hell is going wrong

EDIT: After playing around with that code a little I noticed when a difference made it not work properly. Just try running it yourself, guys, it's not like it can be a virus!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Type 1: doesn't work how I think it will
#include <iostream>
#include <fstream>
using namespace std;

ifstream AnInputStream;
ofstream AnOutputStream;
string displayer;
string pauser;
string sFilePath = "\\\\<MyComputersName>\\Users\\Public\\YourName.txt";
string Inputted;
string aString = "SomeoneElse";
bool repeater = true;

void ReadTheFile() {
     AnInputStream.open(sFilePath.c_str());
     getline(AnInputStream,displayer);
     AnInputStream.close();
     cout << displayer << endl;
}

int main() {
    Repeating:
    ReadTheFile();
    if (repeater) {
                  AnOutputStream.open(sFilePath.c_str(),ios::trunc);
                  AnOutputStream << aString;
                  AnOutputStream.close();
                  repeater = false;
                  goto Repeating;
    }
    getline(cin,pauser);
    return 0;
}



// Type 2: does work how I think it will
#include <iostream>
#include <fstream>
using namespace std;

ifstream AnInputStream;
ofstream AnOutputStream;
string displayer;
string pauser;
string sFilePath = "\\\\<MyComputersName>\\Users\\Public\\YourName.txt";
string Inputted;
string aString = "SomeoneElse";
bool repeater = true;

void ReadTheFile() {
     AnInputStream.open(sFilePath.c_str());
     getline(AnInputStream,displayer);
     AnInputStream.close();
     cout << displayer << endl;
}

int main() {
    AnOutputStream.open(sFilePath.c_str(),ios::trunc);
    AnOutputStream << aString;
    AnOutputStream.close();    
    Repeating:
    ReadTheFile();
    if (repeater) {
                  repeater = false;
                  goto Repeating;
    }
    getline(cin,pauser);
    return 0;
}


And what is a string stream? (I WILL look it up later even if you don't answer that.
Last edited on
closed account (4Gb4jE8b)
see here's the thing. because goto directly interrupts the process of the compiler and runtime, it could still be the reason for your problem. it has unexpected results and errors at times. just google why not to use goto and read some of it. also, while i was editing my previous post you commented, so be sure to go back and read it

a string stream is a stream of information that can be dynamically changed. It's input is cumulative and can be used for quite a bit.

the error here is that you erase all of the information in the file before trying to read the file in your function called readthefile() in the first example.

ie

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
ifstream AnInputStream;
ofstream AnOutputStream;
string displayer;
string pauser;
string sFilePath = "\\\\<MyComputersName>\\Users\\Public\\YourName.txt";
string Inputted;
string aString = "SomeoneElse";
bool repeater = true;

void ReadTheFile() {
     AnInputStream.open(sFilePath.c_str());
     getline(AnInputStream,displayer);
     AnInputStream.close();
     cout << displayer << endl;
}

int main() {
    Repeating:
//and then you go here to read information in the file
    ReadTheFile();
    if (repeater) {
//this line deletes and information in the file
                  AnOutputStream.open(sFilePath.c_str(),ios::trunc);
                  AnOutputStream << aString;
                  AnOutputStream.close();
                  repeater = false;
                  goto Repeating;
    }
    getline(cin,pauser);
    return 0;
}
Last edited on
I know that line 23 deletes the information. But, line 14 displays it first, and line 24 puts new information into it.
I will try it with a stringstream and a while loop and get back to you.

EDIT: Wait, will a stringstream still work with the .c_str() property I use to use the filepath? Or won't it need that property for the .open() method?

EDIT2: And how is the stringstream easier? The only difference I'd see is that I would use '<<' instead of '+'.

EDIT3: Sorry if all these edits are confusing you. It's because I'm using someone else's computer so I am trying to do it in short bursts, so I forget stuff. Anyway; I just tried the while loop on my code in my second post using a bool that was never turned to false to check. Same result. (Also, thanks for letting me know about the goto)
Last edited on
closed account (4Gb4jE8b)
stringstream just uses the .str() instead of the c_str() do you really need the null character?

when working with buffer information (such as loading the entire contents of a file into memory)

while(fstream.good())
{
char any = cin.get();
stringstream << any;
}

loads the whole file into memory because of the cumulative effect. strings themselves cannot do this.

technically if you load a string into a open() method, you don't need to append the c_str() in the first place.

and i see what you mean for the statement about putting the data back in. my bad, didn't see that at first
What do you mean null character?

I have tried putting just the string variable name in, and it doesn't work. If I were to use actual string quotation marks it does work. I assume this is because it technically only accepts a character array, but it can read something in quotation marks character by character like a character array, instead of reading a variable to point it somewhere else.

And what does that while function you posted actually do? To me it looks like that when the file is loaded properly by the filestream, it will accept user input and save it to 'any'. But this doesn't help me at all, because I need the user to input the directory for the filestream to open. So, maybe it does something else. (EDIT: Oh, is "cin" supposed to be "fstream"? So it reads the file character by character and puts it first into a character variable then adds it to the end of a stringstream. Isn't that a round-about way of achieving the same thing?)

And I've tried a CopyFile method to copy the file to another location with a different name, and it's the same result (Works first time, but doesn't later). I went and read the text file using notepad to check whether it copied properly. So I am thinking that there is something wrong with how I request the file. (I've also used cout to display the directory variable before getting the file and it is always how I expect)
Last edited on
closed account (4Gb4jE8b)
yes cin.get was supposed to be fstream.get, my apologies.

a null character is '\0' which is what c_str does, it adds one to the total length of the string and puts that character in the new spot.

the sample code i posted reads what is inside a file character by character until the end of the file. At this point i'm not sure why it's not repeating endlessly.

1
2
3
4
5
6
7
8
9
10
11
    Repeating:
//you should hit this function twice, is that not happening?
    ReadTheFile();
    if (repeater) {
                  AnOutputStream.open(sFilePath.c_str(),ios::trunc);
                  AnOutputStream << aString;
                  AnOutputStream.close();
//this means you'll only repeat it once though, because you only access this if statement once
                  repeater = false;
                  goto Repeating;
    }
I know it runs it twice, I just didn't want to put the code in twice, and I like to practise using the bool as a bool instead of a string. (Where you just put a bool var in without any equality operators)

It's running twice alright...
I think I need to clarify myself.
I have now made an application that can test how I do my filestreams.

http://pastebin.com/M1U9fkfk
EDIT: I haven't tested that stringstream clearing method.

I can always write to a file, but I can only read once (all the other times it doesn't change the value of the 'displayer' variable. Maybe it keeps what it thinks is inside the file in it's memory rather than reading it again?).

And I've even used those stringstreams you were talking about.
It just doesn't work, even if I target it through my harddrive.

Surely someone can look at this and tell me why?
Or at least work it out?
Last edited on
closed account (4Gb4jE8b)
in your while statement you don't need the if statement, it's redundant
getline includes the newline character which is why it wasn't working for your switch statements. If you just use cin<< testmethod1 where testmethod1 is an int, it should work fine.

"//yes, I'm aware I'm changing a string to an int by telling the program to interpret it as a c string. getline just doesn't seem to work if I make testMethod2 an int variable."

c_string doesn't make it an int. atoi makes it an int.

as for your actual problem, i'll get back to you later today if nobody else does.
If you mean the if statement in the ReadNameFile() function, then, no it's not; the switch statement will determine whether another loop is to be performed, if it's not, then we don't want to display anything, since no file will be opened.
I read on another post here on the forums that just using "cin <<" can also cause problems because of it being a stream. Things like it only recording up to the first space and stuff...
I know c_str doesn't make it an int, but I have to tell atoi that my string is a char before it will turn it into an int.
closed account (4Gb4jE8b)
1
2
3
4
5
6
7
8
if(repeater){
while (AnInputStream.good()) {
                                c = AnInputStream.get();
      //this if statement is redundant, to access the while loop this has to be true
                                if (AnInputStream.good()) {
                                                         displayer << c;
                                }
                          }

to access this part in the first place (the very first while statement in readfile()) repeater has to be good (which is fine), then it has to be good for this internal while statement (redundancy). then to further access this while statement, aninputstream must be good (which is fine), then to save the information aninputstream must be good (redundancy). And then directly after this, you check if repeater is good while still inside your while statement (further redundancy). There's your 2x redundancy, a triple check of repeating, and a double check of aninputstream.

here's the thing about cin. It doesn't do type checking, so if you store a string into a variable that's supposed to be an int, you're going to have some issues. But you said this is only between you and a few friends yeah? then it shouldn't be a problem. If you're worried about professional issues later refer to this http://www.cplusplus.com/forum/articles/6046/

To your last statement, i see. I was just making sure you understood what and how you were doing what you were, not just mindlessly using code. My apologies if this insulted you, but i've seen quite a few do so and it bothers me greatly to try and help people who do this and have no care in actually learning.

I don't know how your network works, but this doesn't work on mine. Just letting you know.

I'm getting compiler errors in cases 5-8 for your first argument as not being of the type const char. I've deleted these lines as they don't pertain to the actual problem at hand (which is as i believe the program not making infinite loops and the program not continuously reading)

my specific compiler recommends not using foped(). I've never used it so i can't say you're using it wrong.

i have a feeling it's because you're trying to use the same fstream variable to open multiple different files. That's usually a big no-no, just because things can get very confusing very fast (as it kind of has here). I've separated it into different in and out files. But have tested it to work if you only use one finput var.

I edited a lot, not only the redundancies, but i made it work as well. Because of ios::trunc however it will only store one username at a time, just letting you know.

is yourname.txt stored on both the hard drive and the network? if so why? remember conciseness is very important, and redundancy is a very big no-no. if you can't think straight one day DO NOT spend that day programming, spend the time outlining what you need and how you plan on getting there. Don't spend the time programming in circles and making yourself more confused. It will only hurt you more in the long run when you ask for help or someone else has to read your code. And trust me, i was very close to just starting a new script and doing what you want myself :P I know i'm saying all this, and it might embarrass you or something, but don't be code shy, it's very important to learn how to code so other people can read it. Very little programming is done by oneself.

i took out the way you created your files, as the way you continued to use your file would create them if they weren't already made (trunc does that).

for the version i edited that did work on my computer.
http://pastebin.com/YxmD0nyg
Last edited on
You have missed very nearly the most important comment of my entire code, which I COULD have made you take more notice of, but I assumed enough knowledge of Windows for you to understand it. The one on line 14. There are only 2 files, not 4, even though there are 4 filepaths.
I only ever refer to the two files: YourName.txt and TheirName.txt.
I access each file in two different ways; one by requesting the information as a user of the computer, or two requesting the information as a network user. (2 * 2 = 4)

Thanks for pointing out that FilePath error for options 3 and 4 of the main function.
[edit]No, wait. Stop trying to confuse me. I'm writing two different things to the same file. This is a core element of what I am trying to fix! I want a single file to change, and to be able to read the file before and after the change and to be able to see a difference! I use the copy method to make the other file have a value in it![/edit]

Using the cin method you propose makes it unable to read spaces, so thanks, but no thanks.

As for the redundancies; did you notice that you made it 12 lines longer? There is only one redundancy that I will admit to; checking if the file is good in the while loop that depends on it being good.
Of course; because I'm having an error due to something to do with reading the file, I wanted to make sure it wasn't the reading action itself that was causing the error. So, that would answer that question for me. (The answer was no, it's definitely just the file stream thing)

I made it check the bool 'repeating' again so that it was easier for me to read, and I assume the compiler too. Rather than having 4 sets of instructions that are all the same; I gave it 1 set that had an extra step. Isn't that the purpose of a function? You COULD have all your code in your main function in one file; but that is hard for you to read and hard for the compiler to read. It adds complexity when you are modifying anything. I personally think it good practice. (Possibly not when it relates to 3 or 4 lines, however...)

I understand that each filestream SHOULD only be used for 1 file, but if it's only because it confuses the programmer, then it should technically work when it's coded right even if you use 1 filestream to read 1000 different files. So is mine not coded right? If not, why?

As for why I would want to copy in those different ways; I wanted to test if it's my computers security restrictions. And also as an alternative to just writing to both files.
As I have already mentioned, with one version of my testing codes (before I added options during run-time) if I targeted the files through the harddrive I could read them more than once, but if I targeted them through the network I couldn't. I have been unable to replicate this error.

I refer you now to my post where I showed two alternative codes. One that worked for me and one that didn't. None of this explains that.

I didn't find any of your posts offensive, except that last one (just a teeny bit). But I still respect your opinion and would appreciate continued help...

[edit]And after compiling your code (which needed to have sYourName defined BEFORE USING IT) I realised that your code does nothing. It literally has only one working function; and that is to write "Multihunter" to YourName.txt using the harddrive. All the others fail because they are referencing the wrong place and also have the wrong descriptions of what they do. Not to mention trying to use options 5-8 cause it to close down because your compiler doesn't have windows.h included in it. You've proven; nothing. Explained nothing and I'm pretty sure you don't understand my problem at all........ that was mean.... I'm sorry I'm angry right now, but this filestream business seems to make me angry. Code that doesn't work makes me angry. Especially code that other people look at, and imply should work regardless.
Oh yeah, and the one read function that works still only works once. Which is what this whole thread was supposed to be about fixing![/edit]
Last edited on
I got it working; apparently however I was doing it made some kind of non-critical error, so I used fstream.clear() just before closing it and I could use the fstreams properly.
Oh, and when I added the clear function to the ifstream before it closed, the ofstream started doing the same thing. So I added it to that one too...
For example;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// opening and closing a file
#include <iostream>
#include <fstream>
using namespace std;

int main () {

  ifstream infile;

  infile.open ("test.txt");

  // >> i/o operations here <<

  infile.clear();// this line here is the only difference from the ifstream::close example on the reference page on this website. And is what allows using a fstream more than once.
  infile.close();
  return 0;
}
Last edited on
closed account (4Gb4jE8b)
Good sir, I am insulted and as such you will from now on see a callous tone from me.

"using the cin method doesn't include spaces"

And you need spaces for holding an int why?

"As for the redundancies, did you notice you made it twelve lines longer?"
Did you notice that i also wrote an error checker (see lines 48, 59, 70, and 81) for you and a guaranteed way of knowing exactly at what point the error occurs (see lines 41, 52, 63 and 74 on the link at the bottom of the post)? Or what about the slightly more readable Input/Output stream usage? And due to the way you were trying to use the switch statement it DIDN'T WORK IN THE FIRST PLACE and that you had unnecessarily declared global variables that could well cause memory leaks? Yeah I noticed those. Oh and i fixed the unnecessary use of displayer as you were just outputting it to the console anyway. but in being reasonable, you are new to stringstreams, so I can't honorably hold that against you. I will say that in my usage the best part/use of stringstreams is their ability to merge multiple formats such as string and char and int into one filename or one area of general data which can then be used for parsing or what have you.

"sYourName"

I didn't use that variable, in fact it's not even in the script i posted. Though i will admit that i made the last edit with the computer names before i posted the code for my anonymity, and it would cause an inability to compile, still, an easy fix automatically caught, definitely nothing to get worked up about.

i specifically said in my previous post that options 5-8 didn't work on my compiler

"I'm getting compiler errors in cases 5-8 for your first argument as not being of the type const char. I'VE DELETED THESE LINES as they don't pertain to the actual problem at hand (which is as i believe the program not making infinite loops and the program not continuously reading)"

But you know what? I even fixed the copy statements, like i said you had an argument error. const char* does not make a direct translation to LPCTSTR. Causing my specific IDE to be unable to compile (and in all likelihood yours unable to work even if it did compile)

http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/1b994ff3-da28-46a8-90ef-6c7fab8dcd21/

My code DID have windows.h included in it, maybe you should check what exactly you copied before you start blaming the person trying to help you of incompetence?

I understand that there were only supposed to be two files referenced. That made sense. I also specifically said that your code didn't work on my network, and caused debug errors. And as such moved those so as to be able to work. My apologies for not moving back or mentioning- oh wait i did mention it and again a very easy fix.

After further research, the way you try and connect to the network makes no sense at all. But that's not my problem anymore, because after this post, i'm done here.

And to your last statement, what compiler are you using, because it works perfectly in reading multiple times on my compiler: MS VS 2010. WHICH WAS WHAT YOU ASKED THANK YOU VERY MUCH. Usually speaking, it's a good idea to include what compiler you're using anyway because different compilers give different results. For example, Dev C++ is known to be a generally bad IDE, and upon testing compilation in dev c++(i have it as a failsafe compiler, ie if it doesn't work there it may not be as portable to other systems) it didn't work. Might i suggest Microsoft visual studio 2010 express?

I'm sorry for your stress, but i honestly found that last statement very rude and was only convinced by a friend to even stay and try and help any further.

http://pastebin.com/wKPfMjcm

that's the code that i've now further edited for your convenience. I've removed the comments about what i've done so you can go ahead and do those since I apparently did them wrong.

Thank you for your time, and your welcome for spending over 3 hours working on your project in total. I bid you adieu.
Topic archived. No new replies allowed.