Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

I/O to and from files

I have taken two semesters of C++ at the local college but nobody there will give me a straight answer as to how to open files, read the characters(in ASCII), alter them, then output them to a different file. I have the center section down, now all I need is the ability to get the input from a file, and then output the other way. I have gotten this so far.

::-------------------------------------------::

#include
#include

using namespace std ;

ifstream IN ("blah"); // keep receiving parse error here
ofstream OUT ("haha"); // keep receiving parse error here

int main()
{
cout<<"What's Up Doc?"<<endl;//just to see if the opening worked
system("pause");
return(0);
}

Comments

  • LundinLundin Member Posts: 3,711
    : I have taken two semesters of C++ at the local college but nobody there will give me a straight answer as to how to open files, read the characters(in ASCII), alter them, then output them to a different file. I have the center section down, now all I need is the ability to get the input from a file, and then output the other way. I have gotten this so far.
    :
    : ::-------------------------------------------::
    :
    : #include
    : #include
    :
    : using namespace std ;
    :
    : ifstream IN ("blah"); // keep receiving parse error here
    : ofstream OUT ("haha"); // keep receiving parse error here
    :
    : int main()
    : {
    : cout<<"What's Up Doc?"<<endl;//just to see if the opening worked
    : system("pause");
    : return(0);
    : }
    :

    I think the names IN and OUT might have been taken already from inside one of those h-files. Try:

    ifstream in ("blah"); // keep receiving parse error here
    ofstream out ("haha"); // keep receiving parse error here

  • DonotaloDonotalo Member Posts: 715
    : : I have taken two semesters of C++ at the local college but nobody there will give me a straight answer as to how to open files, read the characters(in ASCII), alter them, then output them to a different file. I have the center section down, now all I need is the ability to get the input from a file, and then output the other way. I have gotten this so far.
    : :
    : : ::-------------------------------------------::
    : :
    : : #include
    : : #include
    : :
    : : using namespace std ;
    : :
    : : ifstream IN ("blah"); // keep receiving parse error here
    : : ofstream OUT ("haha"); // keep receiving parse error here
    : :
    : : int main()
    : : {
    : : cout<<"What's Up Doc?"<<endl;//just to see if the opening worked
    : : system("pause");
    : : return(0);
    : : }
    : :
    :
    : I think the names IN and OUT might have been taken already from inside one of those h-files. Try:
    :
    : ifstream in ("blah"); // keep receiving parse error here
    : ofstream out ("haha"); // keep receiving parse error here

    to check whether the file is opened or not, use the following code:
    [code]
    #include <iostream>
    #include
    using namespace std;

    ifstream in("blah");
    ofstream out("haha");

    int main()
    {
    if (!in) cout << "Cannot open the input file.
    ";
    if (!out) cout << "Cannot open the output file.
    ";
    system("pause");
    return 0;
    [/code]
  • LundinLundin Member Posts: 3,711
    : : : I have taken two semesters of C++ at the local college but nobody there will give me a straight answer as to how to open files, read the characters(in ASCII), alter them, then output them to a different file. I have the center section down, now all I need is the ability to get the input from a file, and then output the other way. I have gotten this so far.
    : : :
    : : : ::-------------------------------------------::
    : : :
    : : : #include
    : : : #include
    : : :
    : : : using namespace std ;
    : : :
    : : : ifstream IN ("blah"); // keep receiving parse error here
    : : : ofstream OUT ("haha"); // keep receiving parse error here
    : : :
    : : : int main()
    : : : {
    : : : cout<<"What's Up Doc?"<<endl;//just to see if the opening worked
    : : : system("pause");
    : : : return(0);
    : : : }
    : : :
    : :
    : : I think the names IN and OUT might have been taken already from inside one of those h-files. Try:
    : :
    : : ifstream in ("blah"); // keep receiving parse error here
    : : ofstream out ("haha"); // keep receiving parse error here
    :
    : to check whether the file is opened or not, use the following code:
    : [code]
    : #include <iostream>
    : #include
    : using namespace std;
    :
    : ifstream in("blah");
    : ofstream out("haha");
    :
    : int main()
    : {
    : if (!in) cout << "Cannot open the input file.
    ";
    : if (!out) cout << "Cannot open the output file.
    ";
    : system("pause");
    : return 0;
    : [/code]
    :


    This has been up before, I would not advise to use the ! operator, sence it only checks if the buffer is valid or not (equal to some member function in ifstream I can't remember the name of) and not if the buffer is useful.

    Here is an example of when the ! operator goes wrong:

    [code]
    #include <iostream>
    #include
    using namespace std;

    int main()
    {
    ifstream fin;

    /* fin is -not- set here */

    if(!fin)
    cerr << "The file isn't open" << endl;
    else
    cout << "The file is just fine, go ahead and read from it" << endl;

    if(!fin.is_open());
    cerr << "On second thought, it isn't open at all..." << endl;

    return 0;
    }

    [/code]
  • DonotaloDonotalo Member Posts: 715
    Strange! :-o

    how only the following code checks whether a file is successfully opened or not, or whether the buffer is valid or not?!
    [code]
    #include
    #include
    using namespace std;

    ifstream in("inputfile");

    int main()
    {
    cout << "Not printed?
    ";
    return 0;
    }
    [/code]
  • blake_gibsonblake_gibson Member Posts: 8
    Thanks guys I really appreciate it. I have not tried it yet but those do make sense. (The ideas). I will definently get back to you once I know for sure. Hope to post many more question, and hopefully in the future return the favor!
  • LundinLundin Member Posts: 3,711
    : Strange! :-o
    :
    : how only the following code checks whether a file is successfully opened or not, or whether the buffer is valid or not?!
    : [code]
    : #include
    : #include
    : using namespace std;
    :
    : ifstream in("inputfile");
    :
    : int main()
    : {
    : cout << "Not printed?
    ";
    : return 0;
    : }
    : [/code]
    :


    It doesn't.
  • blake_gibsonblake_gibson Member Posts: 8
    [b][red]This message was edited by blake_gibson at 2005-9-2 17:59:52[/red][/b][hr]
    After about three hours of fooling around with this program, this is what I came up with. Now that is it inputting from the file,
    I can't get it to output to haha.txt !!! Any suggestions appreciated!=======================================================================

    #include
    #include
    using namespace std ;

    ifstream blake ("blah.txt");
    ofstream sarah ("haha.txt");

    int main()
    {
    char middle; // declare char to use

    if (!blake) cout << "Cannot open the input file." << endl; //check
    if (!sarah) cout << "Cannot open the output file." << endl; //check

    while(!EOF) //stop at the end of file
    {
    blake>>middle; //input char from file
    sarah<<middle; //output char to file
    }
    system("pause");
    return 0;
    }


  • blake_gibsonblake_gibson Member Posts: 8
    well shortly after I got done doing my last post, I realized what I did wrong. Now that I can get the output back to the output file, can someone show me how to get the spaces over there too. Whatever it takes is cool with me. Thanks in advance

  • DonotaloDonotalo Member Posts: 715
    [b][red]This message was edited by Donotalo at 2005-9-2 22:35:30[/red][/b][hr]
    : #include
    : #include
    : using namespace std ;
    :
    : ifstream blake ("blah.txt");
    : ofstream sarah ("haha.txt");
    :
    : int main()
    : {
    : char middle; // declare char to use
    :
    : if (!blake) cout << "Cannot open the input file." << endl; //check
    : if (!sarah) cout << "Cannot open the output file." << endl; //check
    :
    : while(![red]EOF[/red]) //stop at the end of file
    : {
    : blake>>middle; //input char from file
    : sarah<<middle; //output char to file
    : }
    : system("pause");
    : return 0;
    : }

    [red]EOF[/red] of what?? you should mention that!

    EOF is a #defined non-zero value (probably -1). so !EOF makes it 0 and [blue]while[/blue] gets 0 as its condition. so it will never enter the loop body!

    [blue]>>[/blue] and [blue]<<[/blue] operators skips white space characters. here is the modified code:
    [code]
    #include <iostream>
    #include
    using namespace std;

    ifstream blake ("blah.txt");
    ofstream sarah ("haha.txt");

    int main()
    {
    char middle; // declare char to use

    if (!blake) cout << "Cannot open the input file." << endl; //check
    if (!sarah) cout << "Cannot open the output file." << endl; //check

    while(!blake.eof()) //stop at the end of file
    {
    blake.get(middle);//input char from file
    if (!blake.eof())
    sarah.put(middle);//output char to file
    }
    system("pause");
    return 0;
    }
    [/code]


  • blake_gibsonblake_gibson Member Posts: 8
    Thanks alot....I really appreciate it.
Sign In or Register to comment.