Thread: Clearing a String

  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
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: solution...

    Originally posted by Staz

    ...

    int main()

    ...
    hello, internet!

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, 04:05 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01: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, 06:49 PM