Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,067 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,593 people online right now. Registration is fast and FREE... Join Now!




I need help writing a input/output file

 
Reply to this topicStart new topic

I need help writing a input/output file

tanai21
21 Apr, 2008 - 07:17 PM
Post #1

New D.I.C Head
*

Joined: 21 Apr, 2008
Posts: 1

I have being trying to do an input/output file using c++ i can not even get the basic code to work. I don't know what I am doing wrong. My code compiles but it does not open the file that I have saved in notepad. Can someone please help me?
User is offlineProfile CardPM
+Quote Post

jjsaw5
RE: I Need Help Writing A Input/output File
22 Apr, 2008 - 04:02 AM
Post #2

I vill break you
Group Icon

Joined: 4 Jan, 2008
Posts: 1,392



Thanked: 6 times
Dream Kudos: 125
Expert In: HTML, CSS, Database,

My Contributions
QUOTE(tanai21 @ 21 Apr, 2008 - 10:17 PM) *

I have being trying to do an input/output file using c++ i can not even get the basic code to work. I don't know what I am doing wrong. My code compiles but it does not open the file that I have saved in notepad. Can someone please help me?


post your code so we can take a look at it
User is offlineProfile CardPM
+Quote Post

Lizardking
RE: I Need Help Writing A Input/output File
22 Apr, 2008 - 12:08 PM
Post #3

New D.I.C Head
*

Joined: 1 Apr, 2008
Posts: 10


My Contributions
ugh...input and output files are annoying if you dont get them. Anyways, Can someone explain to me how ifstream and ofstream work. And also do you have to make a file first before making the code? or does the compiler does it by itself?
User is offlineProfile CardPM
+Quote Post

jinroh00
RE: I Need Help Writing A Input/output File
22 Apr, 2008 - 06:58 PM
Post #4

New D.I.C Head
*

Joined: 6 Apr, 2008
Posts: 15

QUOTE(Lizardking @ 22 Apr, 2008 - 04:08 PM) *

ugh...input and output files are annoying if you dont get them. Anyways, Can someone explain to me how ifstream and ofstream work. And also do you have to make a file first before making the code? or does the compiler does it by itself?

I have a basic example of ifstream and ofstream below with comments to help example what is being done hope this helps explain how to use fstream a little better.
CODE

//Demonstrating the use of input/output file
//Normally the program will execute but will not display anything.
//U can make the program display something when its finished using
//cout << "Program is done file read and outputted to file" << endl;
//something to that nature
//***********************************************

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>    //this header allows me to use files in the program

using namespace std;
int main ()
{
    //helps the program understand wat is being read in the file
    string firstInitial;
    string lastName;
    float socialNum;

    ifstream inData;
    //this is the prototype needed to start the read of the input file

    ofstream outData;
    //this is the prototype needed to have ur program output ur data into an output file
    //this actually creates a new file.
    
    inData.open ( "example.txt" );
    //the inData is being defined and being opened by the program.
    //note the name in quotes in the ( ), that is the file i made in order to work in the program.
    //the file must be saved inside the same folder that u saved ur code for the program.

    outData.open ( "newExample.txt" );
    //the outData file is made with this instruction and u must name it differently than the inData file.
    //the program will save this file with ur source code and inData files.

    inData >> firstInitial >> lastName >> socialNum;
    //this tells the program what is being read in the file.
    //the names come from wat i defined at the beginning of int main.

    cout << fixed << showpoint;    //this is to read the number as how its read and not by sci fig notation. this is done with the #include <iomanip> header
    outData << socialNum << " " << lastName << " " << firstInitial << endl;
    //for the outData u can manipulate how the program outputs the data being read.

    inData.close ();
    outData.close ();
    //always remember to close both inData and outData files
    return 0;
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 06:49PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month