Thread: How read this file into array without comma?

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

    Question How read this file into array without comma?

    Hello all:

    My program need to read files with numbers as:
    1,171.11
    1,159.36
    1,154.05

    Because these numbers can not be calculated in C/C++, I need to store then in the arrays as:
    1171.11
    1159.36
    1154.05
    that is without commas.

    How read numbers in a file into program arrays taking away commas?

    Could you help me?

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    int main()
    {
    
    	string example; 
    	example = "123,456.00";
    
    	int pos;
    
    	pos = example.find(',',0); //Searches for "," in string starting at position 0
    						 
    												 
    	/* If the string does not have the char in it, then there is nothing to delete
    	This only deletes if the 'not found' value is not present */
    	
    	if ( pos != string::npos) 
    	{
    
    	example.erase(pos,1); //Erases from string, starting at position pos (returned by search)
    	}
    
    
    	cout << example;
    
      return 0;
    }
    For more info on using strings (which are arrays):
    http://www.msoe.edu/eecs/ce/courseinfo/stl/string.htm

    This will only find the first occurrence of the comma, so if you might have more than one comma (such as 123,456,789.00) you can just put it in a loop.
    Last edited by Enahs; 09-30-2005 at 08:08 AM.

  3. #3
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    This isn't perhaps the prettiest example but it serves your purpose. Be careful of the EOF. Best to avoid using it if you can.

    Code:
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
        /*
        list.txt
        -----------------
        1,171.11
        1,159.36
        1,154.05
        */
        
        fstream file_pointer;
        file_pointer.open("list.txt",ios::in);
        
        do
        {
            char array[81];
            char barray[81];
            file_pointer>>array;
            
            int size = strlen(array);
            int count=0;
            int i;
            for( i=0; i<size; i++)
            {
                if (array[i]!=',')
                {
                    barray[count]=array[i];
                    count++;
                } 
            }
            barray[i-1]='\0';//get rid of the junk which may be present
            float f;
            f=atof(barray); //convert to float
            cout<<f;//you can now perform standard maths on the number
            //cout<<f*2;
            cin.get();
            count=0;        
             
                }while(file_pointer.peek()!=EOF);
                file_pointer.close();
    }

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Man...whatever happened to the days of giving the OP some advice and letting him/her figure it out?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    30

    Question Re:How read this file into array without comma?

    Hello Enahs and Treenef:

    Thank you for your passionate responses to write the program for me. Could you go a step forward to modify your program with following requirements.

    Suppose the file named file.txt contains numbers:
    1,111.11
    2,222.22
    3,333.33
    After reading these numbers and then write them into a new file named file_new.txt, the numbers in new file changed to:
    1111.11
    2222.22
    3333.33

    It means that the numbers in file.txt with commas, but in file_new.txt without commas.

    Could you help again?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Could you go a step forward to modify your program with following requirements.
    Get off your lazy arse and do some bloody work yourself
    Just how much of a spoon-feed do you need?????
    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.

  7. #7
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    oh come on, he will make a good IT manager some day!

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    36
    Ok now you're just being lazy. Go look at the string functions on msdn.

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    30

    Thumbs up Solved

    Now Everything has been solved. Thanks anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read coefficients of linear equations from file into 2d array
    By omaralqady in forum C++ Programming
    Replies: 6
    Last Post: 06-20-2009, 07:39 AM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Read from file to an array
    By brooklyn1126 in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2004, 08:32 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM