Thread: How to write back to a temp file keeping the orginal or modifed org file format?

  1. #1
    Banned
    Join Date
    Aug 2017
    Posts
    861

    How to write back to a temp file keeping the orginal or modifed org file format?

    Code:
    /*
    Write a program that reads an input text file, 
    identifying and auto-correcting the following common typing errors: 
    ‘Space before comma or a full-stop ( period ) ’, ‘Multiple spaces as opposed to 
    a single space’, and ‘Missing spaces’. 
    
    * */
    #include <iostream>
    #include <fstream>
    #include <cstring>
    #include <stdlib.h>
     
     using std::cout;
     using std::endl;
    
    
    int main (int argc, const char **argv)
    {
         
        
        if( argc < 2)
        {
            printf("error: no file [ %s ]\n", argv[argc]);
            exit(1);
        }
    
         
        std::ifstream infile(argv[1]);
        std::ofstream temp;
        temp.open("temp.txt");
        std::string str;
        
        if (infile.is_open()) {
         
        char c;
        int position = 0;
           // get length of file:
        infile.seekg (0, infile.end);
        int length = infile.tellg();
        cout<< "length is = "<<length<<endl;
        infile.seekg (0, infile.beg);
        
        while (infile.get(c)) 
        {
            
            if ((int)c == 32)
            {
                // get cur pos
                position = infile.tellg();
                //move back 2 to get c again to see
                // what was before (curr c) it
                infile.seekg(position-2, std::ios::beg);
                infile.get(c);
                if ( (int)c == 32 )
                {
                    cout<<"Got extra space"<<endl;
                    //put it back where it was before hand
                    infile.seekg(position,std::ios::beg);
                }
                else // if no extra space, still needs to be put back
                    infile.seekg(position, std::ios::beg);
            }
            if ((int)c == 44)
            {
                 
                position = infile.tellg();
                
                infile.seekg(position-2, std::ios::beg);
                infile.get(c);
                if ( (int)c == 32 )
                {
                    cout<<"Got extra space before comma"<<endl;
                    infile.seekg(position,std::ios::beg);
                }
                else
                    infile.seekg(position, std::ios::beg);
            }
            if ((int)c == 46)
            {
            
                position = infile.tellg();
                
                infile.seekg(position-2, std::ios::beg);
                infile.get(c);
                if ( (int)c == 32 )
                {
                    cout<<"Got extra space before period"<<endl;
                    infile.seekg(position,std::ios::beg);
                }
                else
                    infile.seekg(position, std::ios::beg);
            }
             // and it does not work yet. 
            if ( (int)c ==  10) {
            //    cout<<"END LINE ";
                temp << '\n'; }
            else if ( (int)c == 32 )
                temp << " ";
            else
                temp << c; 
        } // end while
        } // end if
        infile.close();
        temp.close();
        //remove(argv[1]);
        //rename("temp.txt", argv[1]);
       //std::ifstream tempfile(argv[1]);
        std::ifstream tempfile("temp.txt");
    
        
        if (tempfile.is_open()) {
         
        while (true)
        {
            tempfile >> str;
            
            if (tempfile.eof()) break;
                cout << str ;
        }
    }
      tempfile.close();
    
    return 0;    
    }
    I am lacking in knowing how to get it back in another file the way it came out, and it is getting extra letters put into that temp file, and as I have discovered this method removes white space and end line. and trying to put it back like I am does not work.
    Code:
    $ ./auto_correct auto_correct_test_file
    length is = 53
    Got extra space before comma
    Got extra space
    Got extra space before period
    Got extra space
    Got extra space
    Got extra space
    Got extra space
    Thiss,wassmyytesttfileeanddhereeweegooagainn
    test file
    Code:
    This , was my  test file .
    and here    we  go again.
    Last edited by userxbw; 11-14-2017 at 04:16 PM.

  2. #2
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    830
    The >> << operators eat whitespace, so that's probably our problem. You could use the get and put methods on your streams as an option.

    Btw, you don't need to cast chars to make comparisons, you can just do:
    Code:
    if (c == ' ') // space
    if (c == ',') // comma
    // etc.
    As for your algorithm, have you considered just seeking forward and keeping track of the last few (relevant) characters? That might be another approach to your goal.

    If I had to do this myself, I would first put it all into a std::string and write the final output to file in a single swoop. But I've never been comfortable with stream operations.
    Last edited by -Adrian; 11-14-2017 at 07:15 PM.

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,738
    I might look again once it is formatted in a readable manner

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Hodor View Post
    I might look again once it is formatted in a readable manner


    there is not ANSI standard to formatting code, it is just fine like it is. if you cannot read it then get some glasses. as well that just shows how limited you really are to coding. so don't bother and I am not playing your childish games go away.
    Last edited by userxbw; 11-14-2017 at 08:06 PM.

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by -Adrian View Post
    The >> << operators eat whitespace, so that's probably our problem.

    Btw, you don't need to cast chars to make comparisons, you can just do:
    Code:
    if (c == ' ') // space
    if (c == ',') // comma
    // etc.
    As for your algorithm, have you considered just seeking forward and keeping track of the last few (relevant) characters? That might be another approach to your goal.

    If I had to do this myself, I would first put it all into a std::string and write the final output to file in a single swoop. But I've never been comfortable with stream operations.
    i've never played with this myself, I gave that peek function a try and got no where, and then did it like this, it works as far as finding the what it is looking for, I casted them only because I wanted to use the numbers.


    Quote Originally Posted by -Adrian View Post
    You could use the get and put methods on your streams as an option.
    (edit)
    I guess back to C in C++
    Last edited by userxbw; 11-14-2017 at 08:16 PM.

  6. #6
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,738
    Quote Originally Posted by userxbw View Post
    don't bother and I am not playing your childish games go away.
    I'm just wondering why you've used all those magic numbers (e.g. c == 32) when it makes the program hard to read and non-portable (just use c == ' ').

    Also, it's probably time you learned to use functions and break down your code into smaller sub-problems to avoid these horrendous blobs of unreadable, unmaintainable and buggy code you're writing.

    Formatting code in a readable style is important.

    But you don't seem to care about any of these things. You're a PROFESSIONAL and you KNOW BEST. Keep wandering along your path of miserable and horrible programming, I really don't care. Just don't expect to ever be even a mediocre programmer if you continue the way you are.

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,738
    Quote Originally Posted by userxbw View Post
    i've never played with this myself, I gave that peek function a try and got no where, and then did it like this, it works as far as finding the what it is looking for, I casted them only because I wanted to use the numbers.
    And why did you want to use the numbers? To highlight even further your stupidity?

  8. #8
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Hodor View Post
    And why did you want to use the numbers? To highlight even further your stupidity?
    you have no idea how much in an A$$ you are making of yourself right now. do you?

    I'm sorry your mommy and daddy didn't love you like they should have so you have this superiority complex and feel that need to cut down anyone you can find to make yourself feel better about yourself.
    Last edited by userxbw; 11-14-2017 at 08:40 PM.

  9. #9
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,738
    Quote Originally Posted by userxbw View Post
    you have no idea how much in an A$$ you are making of yourself right now. do you?

    I'm sorry your mommy and daddy didn't love you like they should have so you have this inferiority complex and feel that need to cut down anyone you can find to make yourself feel better about yourself.
    You're right. Upon reflection it does seem that you're a better programmer than me and write much better C. I will defer to your judgement and try to learn to be a better C programmer by following your example.

  10. #10
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,738
    I've rewritten your program to how I'd write it:

    Code:
    #include <stdio.h>
    
    int is_odd(unsigned n); int is_even(unsigned n); int is_even(unsigned n)
    {    return n == (unsigned)
    0 ? (unsigned) 1 : (int)
    is_odd
    (n-1);
    }
    
    int is_odd(unsigned n) {
    return n
    ==
    0 ? 0
    : (n == 1 ? 1
    : is_even(n-1)); } int
    
    main(void) {
        unsigned num = 12;
    
        printf("%u is "
        "%s\n", num, is_even(num) ? (const char *)"even" : "odd");
    
    return (0);
        }

  11. #11
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Hodor View Post
    I've rewritten your program to how I'd write it:

    Code:
    #include <stdio.h>
    
    int is_odd(unsigned n); int is_even(unsigned n); int is_even(unsigned n)
    {    return n == (unsigned)
    0 ? (unsigned) 1 : (int)
    is_odd
    (n-1);
    }
    
    int is_odd(unsigned n) {
    return n
    ==
    0 ? 0
    : (n == 1 ? 1
    : is_even(n-1)); } int
    
    main(void) {
        unsigned num = 12;
    
        printf("%u is "
        "%s\n", num, is_even(num) ? (const char *)"even" : "odd");
    
    return (0);
        }
    that is good Johnny what a good boy you are, you have shown what is call the dexterity of coding. showing others its flexibility now go show your mommy and tell her what you did, and maybe she will give you a lolly pop and pat you on the head. then you can go and try to make others feel lesser then you so you can feel Superior to them. which is only short lived then you just go out and find someone else to pick on so you can try to feed your poorly structured ego. to make yourself feel better about yourself. ( Superior to others ) .

    superiority complex due to feelings of inferiority

    PS your formatting sucks btw
    Last edited by userxbw; 11-14-2017 at 08:40 PM.

  12. #12
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,738
    All I did was ask you to reformat the code so that it was readable. *shrug*

  13. #13
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Hodor View Post
    All I did was ask you to reformat the code so that it was readable. *shrug*
    you're a psychological mess.

  14. #14
    Registered User
    Join Date
    Jun 2017
    Posts
    156
    In case someone is interested in this topic.
    I think this kind of problem easily leads to a top-down approach.
    Code:
    /*
    Write a program that reads an input text file,
    identifying and auto-correcting the following common typing errors:
    Space before comma or a full-stop ( period ), 
    Multiple spaces as opposed to a single space, 
    and Missing spaces.
    */
    
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <exception>
    #include <stdexcept>
    #include <cstdlib>
    #include <cstdio>
    
    using namespace std;
    
    string get_input(const string& filename);
    string process_input(const string& input);
    void show_output(const string& output);
    void write_file(const string& txt);
    
    int main()
    {
      try
      {
        string input = get_input("input.txt");
        string output = process_input(input);
        show_output(output);
        write_file("output.txt", output);
        return 0;
      }
      catch (const exception& ex)
      {
        cerr << "Error: " << ex.what() << "\n\n";
      }
      catch (...)
      {
        cerr << "Unknown error\n\n";
      }
      system("pause");
      return errno;
    }
    
    string get_input(const string & filename)
    {
      string input;
    
      // TO DO - implement logic here
    
      return input;
    }
    
    string process_input(const string & input)
    {
      string output;
    
      // TO DO - implement logic here
    
      return output;
    }
    
    void show_output(const string & output)
    {
      // TO DO - implement logic here
    }
    
    void write_file(const string & txt)
    {
      // TO DO - implement logic here
    }
    CAUTION: This code is just an example of how things can be done.
    It is neither the only way nor the best way.
    It is not properly tested and might not be correct.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,677
    I think you're wasting your time Hodor with this shrub - though it is entertaining

    He continues to post rubbish, but never listens to any critisism.

    > infile.seekg(position-2, std::ios::beg);
    I'd like to see what happens with this "program" when presented with a file where the first character is a comma.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Append a temp file to a log file
    By Randellk02 in forum C Programming
    Replies: 9
    Last Post: 11-07-2010, 01:46 PM
  2. How to write image data to binary PGM file format(P5)?
    By tommy_chai in forum C Programming
    Replies: 6
    Last Post: 11-03-2007, 10:52 PM
  3. handling file rb+ (write back into self)
    By IsmAvatar2 in forum C Programming
    Replies: 5
    Last Post: 01-23-2007, 09:47 PM
  4. Deleting a temp file
    By Oldman47 in forum C++ Programming
    Replies: 1
    Last Post: 01-02-2007, 07:04 PM
  5. How to read/write 8xp file format...
    By supertoad in forum Tech Board
    Replies: 2
    Last Post: 04-29-2005, 04:18 AM

Tags for this Thread