Cheap and Secure Web Hosting Provider : See Now

How to parse data in a text file?

, , No Comments
Problem Detail: 

I had a question about altering a text file. I have data in the following format.

1 a,b,c 2 d,e 3 a, l, m,n 

Note that some of them have spaces after the coma

Now I have to resort the data in the following format

1 a 1 b 1 c 2 d  2 e 3 a 3 l 3 m  3 n 

Now my plan is to first parse the data, and hold a count of how many data elements there are for each number. (Ex. for the number 1, there are 3 elements). Then I would move down the data by 3 spaces, starting from the bottom. Kinda like moving an array. (The main concern here is space more than time complexity)

Then I would enter each data element at the top.

My first question is, how exactly would I parse the data in a txt file and indicate delimiters such as the coma and the possible spaces.

Second, having done this, how would I just move down a whole line inside a text file down one?

I am very sorry if my question is convoluted. I will gladly clarify anything. Thank you so much for your time! Please let me know if there is anything i need to add. Also, I'm comfortable in both java and c. So advice on either language is useful.

Asked By : Jonathan
Answered By : guillaume girod-vitouchkina

I propose this, more direct to code (in java, c, etc.)

  • read each line (until CR/LF)

  • at begining, read a number, until spaces => number n

  • after that, take next characters of line

  • split them by ,

  • for each, trim (delete space before or after) => strings s1, s2, s3

  • then output n s1 CRLF n s2 CRLF n s3, etc.

Best Answer from StackOverflow

Question Source :

3200 people like this

 Download Related Notes/Documents


Post a Comment

Let us know your responses and feedback