Thread: I/O question

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    27

    I/O question

    Hi there, im currently learning about the <fstream> library

    Basically im to write two programs, one to write to a file(app1), and one to read from the file that was created from the first app(app1)

    heres some code ive written to help explain my question:

    The below code is for the app ive built to create a .txt file called "Student Names" and writes "John" "James" & "Joe" to that file

    Code:
    #include<fstream>
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
    	
    	ofstream sfile ("Student Names");
    	
    	cout << "\nThis program will store some names to a file called 'Student Names'";
    	
    	sfile << "John" << "\nJames" << "\nJoe";
    	
    	sfile.close();
    	
    	return 0;
    	
    }
    the path for the above app is stored in /home/user/workspace/StudentW

    The second program ive written, im trying to read from the "Student Names" file created in the first app, and to print in the consol shell:

    Code:
    #include<fstream>
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
    	char str[50];
    	
    	ifstream sfile ("Student Names");
    	
    	cout << "\nThis program will read the names from 'Student Names' created from the 'StudentW' app";
    	
    	sfile >> str;
    	
    	cin.get();
    	
    	return 0;
    }
    Because the second app is store in /home/user/workspace/StudentR how do i apply the path in the ifstream class?

    Thanks in advance

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You could prepend ../ to both paths, having the file in your workspace directory, perhaps?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    omg yes sorry, ive sorted it now, it was in plain site lol, sorry bout this post again :S

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question about file I/O.
    By The7thCrest in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2009, 05:15 PM
  2. Question about file I/O from a newbie
    By henrik in forum C Programming
    Replies: 4
    Last Post: 11-13-2007, 12:48 AM
  3. Question regarding File I/O
    By mhenderson in forum C Programming
    Replies: 4
    Last Post: 08-03-2006, 12:46 PM
  4. File I/O Question
    By 182 in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2005, 03:03 PM
  5. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 05:14 PM