File I/O

This is a discussion on File I/O within the C++ Programming forums, part of the General Programming Boards category; >> By any chance is there some function that can strip whitespace from a string? There are functions like remove ...

  1. #16
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> By any chance is there some function that can strip whitespace from a string?
    There are functions like remove and erase. You can also use find_first_not_of with " \t\n" as the parameter if you feel confident that those characters will be sorted before the other characters, then use substr or erase to chop of that part.

    What about punctuation? Are you guaranteed that there will be no punctuation in the text?

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    55
    So I wrote this function to remove anything that does not match the pattern string. And it is working.

    Code:
    string no_spaces(string &s)
    {
     int pos ;
     string pattern = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.?:',\n"; 
     while(s.find_first_not_of(pattern) != string::npos)
     {
      pos = s.find_first_not_of(pattern) ;
      s.erase(pos,1) ;
     }
     
     return s ;
    }

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    55
    I am having a problem when it comes to the are_anagrams() fuction because when I call make_upcase() inside it, it does not captialize all of the contents of the string. I think it may be a problem with passing by value vs by reference, but still can't get the right results.

    Code:
    #include <iostream>
    #include <string>
    #include <cctype>
    #include <fstream>
    
    
    using namespace std;
    
    string make_upcase(string &s) ;
    
    int count_words(string &s) ;
    
    bool are_anagrams(string &s, string &s2) ;
    
    string sort (string &s) ;
    
    string no_spaces(string &s) ;
    
    
    int main()
    {
       string input_filename , output_filename ;
     ifstream INFILE ;
     ofstream OUTFILE ;
     
     cout <<"Enter the input filename: " ;
     cin >> input_filename ;
     
     INFILE.open(input_filename.c_str()) ;
     
    
     //Checks if file opened successfully. If not, the program exits.
     if(INFILE.fail())
     {
      cout <<"File " << input_filename << " not opened!" << "Exiting Program!" ;
      system("Pause") ;
      exit(1) ;
     }
    
     //Checks if the input file is empty. If empty, the program exits.
     if(!INFILE)
     {
      cout <<"Error: Input file is empty!" << " " << "Exiting Program." ;
      system("Pause") ;
      exit(1) ;
     }
     
     cout <<"Enter the output filename: " ;
     cin >> output_filename ;
     OUTFILE.open(output_filename.c_str()) ;    
     
     string line, line2, cur_line, next_line ;
     while (INFILE)
     {
      while( getline(INFILE, line) )
      { 
       if(!isdigit(line[0])) //Ignores lines that start with integers                     
       {
        break ;
       } 
      }
      cur_line = line ;
    
      while( getline(INFILE, line) )
      { 
       if(!isdigit(line[0])) //Ignores lines that start with integers                      
       {
        break ;
       } 
      }
      
      next_line = line ;
      
      
      if(!cur_line.empty() && !next_line.empty())
      {
    
      OUTFILE << cur_line << endl << make_upcase(cur_line) << endl 
              << count_words(cur_line) << " characters in the string." 
              << "\n" << "\n" << next_line << endl << make_upcase(next_line)
              << endl << count_words(next_line) << " characters in the string." 
              << endl << "\n" ;
      
      if(are_anagrams(cur_line, next_line))
       {
        OUTFILE << "They are anagrams" << endl << "\n" ;
       }
      else
       {
        OUTFILE << "They are not anagrams" << endl << "\n" ;
       }
    
     }
     
     } // End of while(INFILE) loop
     
    
     
     system("Pause");
     return 0;
    
    }// End main
    
    
    string make_upcase(string &s) 
    //----------------------------------------------------------------------------
    //Creates a new string that is an upper case version of the original string. 
    //Pre-Condition: Original string is lowercase.
    //Post-Condition: New String is all Caps.
    //----------------------------------------------------------------------------
    
    {
     string uppercase_string = s ; //copy of string
     if(! s.empty()) //Does the following if the string is not empty
     {
      for(int i = 0; i < uppercase_string.length(); i++)
      {
       uppercase_string[i] = toupper(s[i]) ; //coverts single character to uppercase
       //uppercase_string.insert(i,letter) ;
      } 
     
     }
     
     return uppercase_string ;  
    }
    
    
    
    int count_words(string &s)  //determines how many words are in the string. 
    {
     return s.length() ;  
    }	
    
    
    
    bool are_anagrams(string &s, string &s2)
    //----------------------------------------------------------------------------
    //Determines whether the strings are anagrams of one another.
    //Pre-Condition: Two strings are passed to the function, original string and 
    //               uppercase string.
    //Post-Condition: If the two strings are anagrams of eachother, the function 
    //                returns true, else it returns false.
    //----------------------------------------------------------------------------
    {
     if(!s.empty())
     {
      make_upcase(s) ;  //Capitalize all characters in the strings since 
      make_upcase(s2) ; //case is ignored with anagrams.
      
      cout << s << endl << s2 << endl ;
      
      no_spaces(s) ;  //Remove all spaces from the strings
      no_spaces(s2) ;
      
      sort(s) ;  //Sort the strings passed to are_anagrams()
      sort(s2);
      
      
        if(s.compare(s2) == 0)  
        {
         return true ;
        }
        else
        {
         return false ;
        }
     }
    } //end of function definition
    
    
    
    string sort (string &s)
    //-------------------------------------------
    //Sorts a string 
    //Pre-condition: Receives an unsorted string
    //Post-condition: Returns a sorted string
    //-------------------------------------------
    {
     char tmp ;
     for(int i = 0; i < s.size(); i++)
     {
      for(int j = 0; j < s.size(); j++)
      {
       if(s[i] > s[j])
       {
        tmp = s[i] ;
        s[i] = s[j] ;
        s[j] = tmp ;  
       }
      } //end of int j for loop
     } //end of int i for loop
      
     return s ;
     
    }//end of function definition
    
    
    string no_spaces(string &s)
    {
     int pos ;
     string pattern = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.?:',\n"; 
     while(s.find_first_not_of(pattern) != string::npos)
     {
      pos = s.find_first_not_of(pattern) ;
      s.erase(pos,1) ;
     }
     
     return s ;
    }

  4. #19
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If you are trying to change the passed in string to be all uppercase, there is no need for the uppercase_string variable. Just modify the passed in string directly.

    The reason you are having a problem is that you are making uppercase_string all uppercase and returning it, but you are ignoring the return value in some cases because you assume the parameter will also be made uppercase.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 04:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21