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

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




importing text

 
Reply to this topicStart new topic

importing text

sms123
post 18 Aug, 2008 - 02:39 PM
Post #1


D.I.C Head

**
Joined: 15 Aug, 2008
Posts: 54


My Contributions


if i had a .txt file and wanted to import the contents, into a console application, what can i do, and no i am not asking for code just asking if there are any functions which import text (assuming it is in the same directory as the .exe) and any tutorials, google isnt helping me.
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 18 Aug, 2008 - 02:44 PM
Post #2


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,440



Thanked 94 times

Dream Kudos: 2625

Expert In: ruling the world.

My Contributions


Search google for a C++ ifstream tutorial.

Basically, you would want to open the file, read it in, line by line, outputting each line as you go. Then close the file.

Simple! smile.gif

EDIT:
Wait, are you using C or C++ ?

If you're using C, just search Google for a C *FILE tutorial. smile.gif

ANOTHER EDIT:
Tell me if you're using C or C++ and I'll explain it smile.gif
User is online!Profile CardPM

Go to the top of the page

sms123
post 19 Aug, 2008 - 01:51 AM
Post #3


D.I.C Head

**
Joined: 15 Aug, 2008
Posts: 54


My Contributions


QUOTE(gabehabe @ 18 Aug, 2008 - 03:44 PM) *

Search google for a C++ ifstream tutorial.

Basically, you would want to open the file, read it in, line by line, outputting each line as you go. Then close the file.

Simple! smile.gif

EDIT:
Wait, are you using C or C++ ?

If you're using C, just search Google for a C *FILE tutorial. smile.gif

ANOTHER EDIT:
Tell me if you're using C or C++ and I'll explain it smile.gif

hey man yh i am using c++ i have got it working but i have a problem when writing to a file, basically, the file is a scoreboard, so everytime you win, it writes "win" in the file and "lose" for when you lose and "tie". my problem is, if the file contains "win" on the first line, an i win again, it is meant to write:
win
win
but instead it just over writes the first one, is there anyway to make it go to the end before writing.
this is the function which is meant to update the score.txt
CODE

void decideWinner(){
    ofstream scorefile ("score.txt");
    char winner = checkWinner();
    if(noOfChoices<9){
        if(winner == 'X'){
            if(menuOption==1){
                cout << "You won." << endl;
                if (scorefile.is_open()){
                    scorefile << "\nwin";
                }else{
                    cout << "Unable to open score file\n";
                }
            }else if(menuOption==2){
                cout << "Player1 won." << endl;
                cout << "Player2 lost." << endl;
            }
            noWinner=false;
        }else if(winner == 'O'){
            if(menuOption==1){
                cout << "You lost." << endl;
                if (scorefile.is_open()){
                    scorefile << "\nlose";
                }else{
                    cout << "Unable to open score file\n";
                }
            }else if(menuOption==2){
                cout << "Player2 won." << endl;
                cout << "Player1 lost." << endl;
            }
            noWinner=false;
        }else{
            noWinner=true;
        }
    }else{
        cout << "Its a tie." << endl;
        if (scorefile.is_open()){
            scorefile << "\ntie";
        }else{
            cout << "Unable to open score file\n";
        }
        noWinner=false;
    }
    scorefile.close();
}
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 19 Aug, 2008 - 02:04 AM
Post #4


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,440



Thanked 94 times

Dream Kudos: 2625

Expert In: ruling the world.

My Contributions


When you open the file, try this:
ofstream scorefile ("score.txt", ios::app);

That will create a new file if it doesn't already exist, and append it to a file if it already exists smile.gif

Try this tutorial, it has a nice little table with explanations of each of the ios states smile.gif
User is online!Profile CardPM

Go to the top of the page

sms123
post 19 Aug, 2008 - 02:18 AM
Post #5


D.I.C Head

**
Joined: 15 Aug, 2008
Posts: 54


My Contributions


QUOTE(gabehabe @ 19 Aug, 2008 - 03:04 AM) *

When you open the file, try this:
ofstream scorefile ("score.txt", ios::app);

That will create a new file if it doesn't already exist, and append it to a file if it already exists smile.gif

Try this tutorial, it has a nice little table with explanations of each of the ios states smile.gif

once again, thanks lol
one last thing tho, what if i want to delete all contents of the file?
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 19 Aug, 2008 - 10:39 AM
Post #6


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,440



Thanked 94 times

Dream Kudos: 2625

Expert In: ruling the world.

My Contributions


Do you still want the file to be available? Like, you have a file called "scores.txt" but you want to clear the contents, leaving an empty txt file called scores?

If that's what you're aiming for, the quickest way would be to create a file over it, using ios::trunc wich will simply replace the existing file with a new one, which will be empty. smile.gif

Or, if you want to remove the file completely, it would make sense to call the remove() function, like so:
remove ("gabehabe.txt");

smile.gif
User is online!Profile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 04:49AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month