Reading a line from a file

This is a discussion on Reading a line from a file within the C++ Programming forums, part of the General Programming Boards category; I have a file it has several lines like this (< Readme >) I need to remove the white spaces ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    4

    Reading a line from a file

    I have a file it has several lines like this (< Readme >)

    I need to remove the white spaces and < , > and put readme stirng
    in a string .The problem is I do not know how many chars are there in every line.

    Thank you
    Code:
    # include<string>
    # include<iostream>
    # include<fstream>
    using namespace std;
    void read ();
    int main()
    {
    
    	read();
    	
    	return 0;
    }
    
    void read()
    {
                                                         		
    	fstream is;
    	is.open("test");
        string st1;
    	char ch;
    	char ch1='<';
    	char ch2='>';
    	while(is.get(ch) != EOF)
    	
      
    
    }
    Last edited by blueguy; 09-24-2005 at 02:37 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Post your code . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    4
    I added my code...Please help me

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    void read()
    {
                                                         		
    	fstream is;
    	is.open("test");
        string st1;
    	char ch;
    	char ch1='<';
    	char ch2='>';
    	while(is.get(ch) != EOF)
    	
      
    
    }
    There's nothing in that while loop!

    So you want to write a function that will read a line from is, and strip off the < and >
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Try something like
    Code:
    cin >> string_variable;
    and then remove the first char and last char of the string.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Perhaps, you could use std::getline to read each line from the file into an std::string buffer, and then retrieve the length of the line read using the string's .length() member. Small reference page: http://www.cppreference.com/cppstring/getline.html

    >> There's nothing in that while loop!

    I'm sure he's fully aware of this. He doesn't seem to have a direction on this and you aren't helping give him one.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    4

    Yes Bade...........

    Quote Originally Posted by dwks
    There's nothing in that while loop!

    So you want to write a function that will read a line from is, and strip off the < and >

    exactely bade .

    If i have some line like < readme > in a file all I want to do is

    read only the contents in < > and store it in vector or string.I can use getline but it takes the entire line which is not I want.


    Thanks

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I think getline is what you want. You can read in the whole line, and then manipulate it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    For manipulating: erase.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 11:39 PM
  2. Reading random line from a text file
    By helloamuro in forum C Programming
    Replies: 24
    Last Post: 05-03-2008, 11:57 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  4. reading file line by line
    By ameet in forum C Programming
    Replies: 7
    Last Post: 09-16-2004, 11:54 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM

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