Thread: editing file

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    1

    editing file

    Hi,
    I am a beginner C++ programing student, and I am stuck with this question that I hope some body can help me with. I am trying to write a program that would read from one file and transfer it to the new file, at the same time editing it also. Editing the file by taking all of the extra white space out, of the old file, and replace it with a single white space. I can get the file to transfer to the new file, but I cannot get it to edite the file. Here is the definition that I got:

    void EditFile(ifstream& InStream, ofstream& OutStream)
    {
    char next;
    InStream.get(next);
    while ( ! InStream.eof())
    {
    if (next == ' ')
    {
    Outstream << " ";
    }
    else
    {
    Outstream << next++;
    }

    InStream.get(next);
    }
    }

    This is part of the codes that I got, I would really appreciated if any one could help me. Thank you very much.

  2. #2
    elad
    Guest
    the program is only viewing one char at a time, which is fine for transfering data. But if you want to edit out any back to back space characters then you need some mechanism to keep track of the last char seen too. Then if current char is a space char and the last char is a space char you will ingnore the current char. Another variable of type char will probably do it.
    Code:
    void EditFile(ifstream& InStream, ofstream& OutStream)
    {
      char current;
      char previous;
      InStream.get(current);
      OutStream << current; //first char is free, can't be back to back spaces
      while ( ! InStream.eof())
      {
         previous = current;
         InStream.get(current);
         if (current != ' ')
        {
            Outstream << current;
        }
        else if(current == ' ' && previous != ' ')//first space saved
        { 
            Outstream << current;
        }
        else if(current == ' ' && previous == ' ')//back to back spaces aren't
        {
           cout << "found back to back space" << endl;
        }
    }

  3. #3
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    First, a tip: when listing code on these boards use the tags 'code' and '/code' (einclosed in square brackets []) before and after, respectively so it will be formatted correctly.

    Try this:
    Code:
    void EditFile(ifstream &fin,ofstream &fout) {
      while (fin.good()) {
        char ch = fin.get();
        if (ch == EOF)
          break;
        fout.put(isspace(ch) ? ' ' : ch);
        if (isspace(ch))
          while (isspace(fin.peek()) && fin.good())
            fin.get();
      }
    }
    When you say 'white space' you are referring to all white space right (including newlines and tabs)? If so, just make sure you're checking for them and not just spaces, otherwise you can just check if it == ' ' instead of isspace().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM