Comma delimited File

This is a discussion on Comma delimited File within the C++ Programming forums, part of the General Programming Boards category; I am trying to use StreamReader to read a file and to separate the lines that I read by commas ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    383

    Comma delimited File

    I am trying to use StreamReader to read a file and to separate the lines that I read by commas (Del).
    I think I have managed to Read the file so I will have the Line in the StringLine.

    Now is my problem how I will put the values to the Vectors with the ->Add method.
    How will I separate out this line by Commas to do this ?


    text1,1.1,2.2
    text2,3.3,4.4


    Code:
    StreamReader^ File1 = gcnew StreamReader( "D:\\Files\\OneFile.txt" );
    
    String^ Del = ",";
    String^ StringLine;
    String^ Text;
    
    List<String^>^ String1 = gcnew List<String^>();
    List<double> Value1 = gcnew List<double>();
    List<double> Value2 = gcnew List<double>();
    
    
    		 
    while ( File1->Peek() >= 0 )
    {
    
    	 StringLine = File1->ReadLine();
    
    	 Day1->Add(      );
    	 Value1->Add(    );
    	 Value2->Add(    );
    }
    Last edited by Coding; 09-29-2008 at 08:50 AM.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    You can split the string via comma. I don't know the CLI syntax but I do know the C# so I will do that instead.
    Code:
    string[] commaArray = StringLine.split(',');
    for(int i = 0; i < commaArray.length; i++){
        Day1.Add(commaArray[i]);
    }//for
    Woop?

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,047
    So you need to find each comma in order, and copy the value starting from the previous comma, to the newly found comma into the new string.

    std::string has a constructor that a string and a range of values to copy over. string::find() will find the first occurrence of character starting from a given index. This can be used to find the position of each comma by setting the starting index to the position of the previous comma.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    383
    prog-bman and King Mir...

    This should be the solution. It seems to work great. Thanks a lot for your help !

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    Anytime. You are welcome.
    Woop?

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Actually, this sounds more like a job for stringstreams, if you were to ask me. But it sounds like you already have the code working, so perhaps someone else with similar may gain more help from my option.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 09:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 01:58 AM

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