Clearing a String

This is a discussion on Clearing a String within the C++ Programming forums, part of the General Programming Boards category; This should work: Code: #include <iostream.h> #include <fstream.h> #include <stdio.h> #include <string.h> void main() { ifstream OpenFile("text.txt"); // Your text ...

  1. #16
    Registered User Staz's Avatar
    Join Date
    Aug 2002
    Posts
    8

    solution...

    This should work:

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdio.h>
    #include <string.h>
    
    
    
    void main()
    
    {
    
          ifstream OpenFile("text.txt"); // Your text file.
          char ch, temp_str[50];
          int cord = 0, last_count = 0;
          while(!OpenFile.eof())
    
          {
    
                OpenFile.get(ch);
                
                   if (ch == '\n') {
                      temp_str[cord] = '\0';
                      cout<<temp_str<<"\n"; /* Instead of printing it to your console you could process it however before preceding. */
                      cord = 0;
                      last_count = 0;                  
                   }                 
                   else {
                      temp_str[cord] = ch;
                      cord++;
                      last_count++;   
                   }
    
          }
    
          OpenFile.close();
          last_count = last_count - 1;
          temp_str[last_count] = '\0';
          cout<<temp_str<<endl; /* Same as line 23 (to process the current string) just for the last line in the text file since there is no '\n' at the end of the line. */
    
    }
    Last edited by Staz; 11-23-2002 at 12:52 AM.

  2. #17
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: solution...

    Originally posted by Staz
    void main()
    Eh...

    *quietly walking away, avoiding the wraith of some of the older members here...*
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #18
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    >*quietly walking away, avoiding the wraith of some of the older members here...*

    Any two, or three, in particular who come to mind?

    * curled up in foxhole awaiting 'incoming' *

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  4. #19
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: solution...

    Originally posted by Staz

    ...

    int main()

    ...
    hello, internet!

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

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 05:05 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02:02 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

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