Read, write, append and delete text file over network.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 000dreamsound000
    New Member
    • Jul 2007
    • 9

    Read, write, append and delete text file over network.

    Hi
    I need help with to access a text file over my local network.
    I need to be able to create,read, write and delete the txt file on a specific machine(s) from any computer over the network. The folder where the txt file should be created is enabled for sharing and allows other users on the network to change files (XP Pro on all machines). My code is as follows:
    [code=c]
    // Writing Local IP to File
    fstream myfile;
    myfile.open ("\\computer_na me\\folder_name \\test2.txt");
    myfile << localIP;
    myfile.close();
    //////////////////////////////////////////////////////////////////
    //Reading from File for Local IP
    string line;
    fstream myfile ("\\computer_na me\\folder_name \\test2.txt");
    if (myfile.is_open ())
    {
    while (! myfile.eof() )
    {
    getline (myfile,line);
    cout << line << endl;
    }
    myfile.close();
    }
    [/code]

    I don't seem to be able to create, read, write, etc the file using
    [code=c]
    fstream myfile;
    myfile.open ("\\computer_na me\\folder_name \\test2.txt");
    [/code]
    It seems like the path is wrong although if I paste the path into 'RUN' I obtain the correct folder/file.
    Can you please help with this. Also I need help on deleting the file after reading.
    Thank you
    S
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by 000dreamsound00 0
    Hi
    I need help with to access a text file over my local network.
    I need to be able to create,read, write and delete the txt file on a specific machine(s) from any computer over the network. The folder where the txt file should be created is enabled for sharing and allows other users on the network to change files (XP Pro on all machines). My code is as follows:
    [code=c]
    // Writing Local IP to File
    fstream myfile;
    myfile.open ("\\computer_na me\\folder_name \\test2.txt");
    myfile << localIP;
    myfile.close();
    //////////////////////////////////////////////////////////////////
    //Reading from File for Local IP
    string line;
    fstream myfile ("\\computer_na me\\folder_name \\test2.txt");
    if (myfile.is_open ())
    {
    while (! myfile.eof() )
    {
    getline (myfile,line);
    cout << line << endl;
    }
    myfile.close();
    }
    [/code]

    I don't seem to be able to create, read, write, etc the file using
    [code=c]
    fstream myfile;
    myfile.open ("\\computer_na me\\folder_name \\test2.txt");
    [/code]
    It seems like the path is wrong although if I paste the path into 'RUN' I obtain the correct folder/file.
    Can you please help with this. Also I need help on deleting the file after reading.
    Thank you
    S
    Shouldn't you make the path like this:

    [code=cpp]
    myfile.open ("\\\\computer_ name\\folder_na me\\test2.txt") ;
    [/code]

    kind regards,

    Jos

    Comment

    • 000dreamsound000
      New Member
      • Jul 2007
      • 9

      #3
      Originally posted by JosAH
      Shouldn't you make the path like this:

      [code=cpp]
      myfile.open ("\\\\computer_ name\\folder_na me\\test2.txt") ;
      [/code]

      kind regards,

      Jos
      Thanks for the quick reply. I had tried ("\\\\computer_ name.....") before and it didn't work. I tried it again but no luck. I can't figure out what I'm doing wrong.
      Thanks for any suggestions

      Comment

      • 000dreamsound000
        New Member
        • Jul 2007
        • 9

        #4
        Seems like it wasn't liking fstream to write to the txt file in the first place.Changed to ifstream and all fine
        Thanks

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by 000dreamsound00 0
          Seems like it wasn't liking fstream to write to the txt file in the first place.Changed to ifstream and all fine
          Thanks
          Did you open your fstream as an output file:
          [code=cpp]
          myfile.open ("\\\\computer_ name\\folder_na me\\test2.txt", ios_base::out);
          [/code]
          ???

          Comment

          • 000dreamsound000
            New Member
            • Jul 2007
            • 9

            #6
            Originally posted by weaknessforcats
            Did you open your fstream as an output file:
            [code=cpp]
            myfile.open ("\\\\computer_ name\\folder_na me\\test2.txt", ios_base::out);
            [/code]
            ???
            No, I didn't use 'ios::' at all. I am just a beginner at C++ and I couldn't tell you what was going wrong. But everything seems fine now.
            Thanks for your help.

            Comment

            • 000dreamsound000
              New Member
              • Jul 2007
              • 9

              #7
              Originally posted by 000dreamsound00 0
              No, I didn't use 'ios::' at all. I am just a beginner at C++ and I couldn't tell you what was going wrong. But everything seems fine now.
              Thanks for your help.
              Also fstream specifies ios::out and ios::in as default. And ofstream-ios::out.
              ifstream-ios::in.
              Cheers

              Comment

              Working...