Results 1 to 4 of 4

Thread: read from a file then output to another file, very basic!.:Resolved:.

  1. #1

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290

    Question read from a file then output to another file, very basic!.:Resolved:.

    Hello everyone!! I have no Idea what I did wrong in the following program, it simply reads data from a file called test.txt located at C:. The data in the test.txt file is the following:
    C 87 89 65 37 98 and it should modify the data and output the data to testavg.out alot located on the C:
    here's my code:
    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    using namespace std;
    
    
    int main(void)
    {
    	int t1,t2,t3,t4,t5;
    	double average;
    	ifstream inFile;
    	ofstream outFile;
    	char studentId;
    	
    	
    	inFile.open("c:test.txt");
    	outFile.open("c:testavg.txt");
    
    	outFile << fixed << showpoint;
    	outFile << setprecision(2);
    
    	cout <<"Processing Data" << endl;
    	inFile >> studentId;
    	outFile<<"Student ID: " << studentId << endl;
    
    	inFile >> t1 >> t2 >> t3 >> t4 >> t5;
    	outFile<<"Test sources: " << setw(4) << t1
    		<< setw(4) << t2 << setw(4) << t3 << setw(4)
    		<< t4 << setw(4) << t5 << endl;
    
    	average = static_cast<double>(t1+t2+t3+t4+t5)/5.0;
    	outFile <<"Average test score: " << setw(6)
    			<< average << endl;
    
    	inFile.close();
    	outFile.close();
    	return 0;
    }
    I think it has somthing to do with the output file, because it doens't seem to create another file, also I thought it was odd when I typed in the following:
    outFile.open() the member function open didn't pop up automatically like it did with inFile.open() and i'm using VC.net
    Thanks for listening!
    Last edited by voidflux; Jul 23rd, 2003 at 10:42 PM.
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    The only problem I see with the code are the invalid paths:
    "c:test.txt"
    and this might even be a forum error. Are you sure you use double backslash ("\\")?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290

    Thumbs up

    THanks CornedBee that worked great! I never knew you had to have a double '\\'
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    That's because a single \ is an escape character. It has a special meaning. Specifically, it turns the character immediatly following into something else, e.g. \n -> newline, \t -> tab, \0 -> NUL-character, \" -> " (not ending the string). Therefore a \ cannot simply be a \. To get a \ you must write \\.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width