CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2002
    Posts
    936

    Converting Ini Text File to CSV

    I have a file where I read data. While the data is in the linear format, but it is not straigt fowardto convert it by simply use carriage return and add comma after. My data looks like

    fiield1=text
    field2=tex
    field1=1
    field1=33
    field1=432
    field1=text

    I have more field1 than field 2 and so on. If I convert it to csv, I can add 0 to the value of field1, assume that I have more field1 than field2. I have more than two fields.

    By using ifstream to read the file and ofstream to write the csv file, how can I solve this problem?

    First I write the header for the csv file

    PHP Code:

    fileOut
    <<"Field 1,"<<"Field 2,"<<"Field 3,"<<"Field 4,"<<"Field 5";
    fileOut<<endl
    The problem I have is to keep reading "fileIn" for one field at a time and write the value to fileOut in a proper format

  2. #2
    Join Date
    Jun 2005
    Posts
    1,255

    Smile Re: Converting Ini Text File to CSV

    I suggest, you would:

    1. Open the input and output file in text mode
    2. Have a loop until the end of the input file
    3. Close the files

    2. In the Loop
    2.1. Initialize an array for your CSV fields
    2.2. Have another loop until the end of the file or the end of CSV line

    2.2. In the second Loop
    2.2.1. Read a line in a buffer
    2.2.2. Split the content of the buffer.
    2.2.3. Put the content in a the right array
    2.2.4. If the last field is read, or end of file,
    2.2.5. then write your CSV line

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured