Thread: Basic file read, file write program

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    17

    Basic file read, file write program

    Hey all,
    I'm trying to write a program which takes in a file called original.dat, which is a source code file, and separates the comments (denoted by // and /**/) from the regular code. The program writes the comments to a file called comment.dat and the code to a file called source.dat.
    Just a little background, I had a God-awful programming I teacher before, and my programming II teacher expects us to be intimately familiar with all aspects of coding.
    I know I have some unused identifiers in there, they are just remnants of other attempts I made.
    I am using the Bloodshed Dev C++ compiler, and I was trying to test this program with some .dat files written in Dev, and all I get out is a bunch of system trash, with lots of files paths included.
    I could use some advice; to my knowledge, this works as I expect. It stores the whole input file into a string, and then goes character by character through the string and checks for '/' and then if it encounters that, checks for another '/' or a '*' to denote comment tags, and then writes them out to the comment.dat file. It also echoes the data written to the screen. But I was unable to test this to my satisfaction.
    Code:
    #include <iostream>
    #include <stdio.h>
    #include <fstream>
    #include <conio.h>
    #include <string>
    
    #define ENDFILE "CTRL-Z"
    #define infile "original.dat"
    #define outfile1 "source.dat"
    #define outfile2 "comment.dat"
    using namespace std;
    
    int outcomments( ifstream &xx);
    int main()
    {
        int line1;
        ifstream originalin;
        ofstream sourceout;
        ofstream commentout;
        
        originalin.open(infile);
        sourceout.open(outfile1);
        commentout.open(outfile2);
        line1=0;
        line1 = outcomments(originalin);
        system("pause");
        return 0;
    }
    
    int outcomments(ifstream &xx)
    { //begin function
    ifstream originalin;
    ofstream commentout;
    ofstream sourceout;
        
    originalin.open(infile);
    sourceout.open(outfile1);
    commentout.open(outfile2);
    string file;
    string comment_slash;
    string comment_slash_star;
    char ch;
    int i,line1;
    line1=0;
    i=0;   
           while(!xx.eof())
           { //begin outer while
           originalin>>file; //stores entire file in string
           if(file[i]=='/')
           {                           //begin outer if
               i++;
               if(file[i]=='/')
               {                       //begin inner if
                   commentout<<'/';
                   while(file[i]!='\n')
                   {                   //begin inner while
                   commentout<<file[i];
                   cout<<'/'<<file[i]; //echo
                   i++;
                   }                   //end inner while
                   commentout<<endl;
               }                       //end inner if
               else
               {                       //begin inner else
                   if(file[i]=='*')
                   {                   //begin inner inner if
                      commentout<<'/';
                      while(file[i]!='/')
                      {                  //begin inner while
                      commentout<<file[i];
                      cout<<file[i]; //echo
                      i++;
                      }                  //end outer while
                   }                     //end inner inner if
               }                         //end inner else
           }                             //end outer if
                   else
                   {
                   sourceout<<file[i]; //if characters are not // or /* then the code
                   }                   //is written to the source file, without comments
           } //end outer while
           
    return 0;
    } //end function

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Opening files twice is probably a bad idea.

    You pass one opened file as a param, then you open them all again inside the function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    17
    yeah, whenever I tried to compile, I got all kinds of errors until I reopened them inside the function. I really don't understand the syntax of the &xx and my professor is of no help.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Although you're not using this, what is it?

    #define ENDFILE "CTRL-Z"

    Ctrl-Z is a KEY combination, not a string.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Lookup reference parameters in your book / lecture notes.

    From what you posted, I think you should open the 3 files in main, then pass 3 reference parameters to your function to do all the file work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #include <iostream>
    #include <stdio.h>
    #include <fstream>
    #include <conio.h>
    #include <string>
    Nothing in your sample code provided requires either of those headers highlighted in red above... however this:
    Code:
    system("pause");
    ...does technically require the cstdlib header.



    Code:
    while(!xx.eof())
    Testing the xx stream's eof flag hmm? Well, there's problems with that. First, for that test to ever mean anything, you'd have to actually do something with the stream, like read from it. Nothing in that while loop of yours reads from the stream so its state will never change and the test is therefore meaningless. Second, there are better ways to do the "loop while there is more stuff to process from our file" reading loop. This typically involves testing the read operation itself instead of testing the end-of-file flag, for example, instead of:
    Code:
    while(!stream.eof())
    {
        stream >> variable;
        ...
    ...it is far better to do:
    Code:
    while(stream >> variable)
    {
        ...
    There are good reasons for doing things that way but I'm a bit tired to try and explain.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  3. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Last program!!
    By buckwheat88 in forum C++ Programming
    Replies: 12
    Last Post: 01-17-2006, 12:31 PM