How to Delimit Comma While Reading a Text File C++

#one

  • D.I.C Regular

Reputation: twenty

  • View blog
  • Posts: 297
  • Joined: xiv-Baronial 12

Reading a comma separated file in C

Posted 07 September 2015 - 09:00 PM

Hullo

I have a file like

one,120,0,12000;ane,126,i,12000;....

I want to read this file and shop the values in a construction

//Structure to store all the transaction related entities     typedef struct Validate_tran     {         int tran_id;         int acc_no;         int dr_cr;         int amount;     }Validate_tran;      //number of transactions(unique/not unique) in the transaction file is assumed to exist 20     Validate_tran tran_list[20];            

So in the end I desire
tran_list[one].tran_id=ane
tran_list[ane].acc_no=120
tran_list[1].dr_cr=0
tran_list[1].amount=12000
abd so on...

I know there is fscanf() with "%[^,]" but that is used for reading characters I guess.
What should I practice?

Thanks and Regards


Is This A Good Question/Topic? 0

  • +

#2 Skydiver User is offline

Reputation: 7916

  • View blog
  • Posts: 26,433
  • Joined: 05-May 12

Re: Reading a comma separated file in C

Posted 07 September 2015 - 09:08 PM

Instead of guessing, have time to read the documentation.

#3 snoopy11 User is offline

Reputation: 1594

  • View blog
  • Posts: 5,010
  • Joined: 20-March x

Re: Reading a comma separated file in C

Posted 07 September 2015 - 11:30 PM

Well at that place are many ways to practice this but y'all dont specify if this is C or C++,
Im going to presume its C,

One possible way.

I would outset fix upward a buffer of type char*,

use fread instead of fgets or fscanf

to read into the buffer the whole file,

to become the size of the file I would utilize struct stat.

Then I would parse the file into the struct Validate_tran

using my own parsing function, which you lot would write yourself.

helpful links.

http://world wide web.cplusplus...due east/cstdio/fread/
http://world wide web.cplusplus...cstdlib/malloc/
http://www.cplusplus...o/FILE/?kw=FILE

#4 #define User is offline

Reputation: 1868

  • View blog
  • Posts: 6,763
  • Joined: 19-February 09

Re: Reading a comma separated file in C

Posted 08 September 2015 - 08:34 AM

Hi, yous could phone call fscanf to read a record (group) into a char buffer, and and so utilize due southscanf to read each field from the buffer string.

#v rnty User is offline

  • D.I.C Regular

Reputation: 20

  • View blog
  • Posts: 297
  • Joined: fourteen-Baronial 12

Re: Reading a comma separated file in C

Posted 30 September 2015 - 07:xvi AM

Thanks guys. I managed to do it.

#6 horace User is offline

Reputation: 768

  • View blog
  • Posts: iii,832
  • Joined: 25-Oct 06

Re: Reading a comma separated file in C

Posted xxx September 2015 - 08:11 AM

View Post#define, on 08 September 2015 - 04:34 PM, said:

Hi, you could call fscanf to read a tape (group) into a char buffer, and and so apply sscanf to read each field from the buffer string.

if the comma seperated format is known you lot tin can read the integer values directly using fscanf() conversion specification
e.one thousand. using scanf()

              Validate_tran v[twenty];     int main(void)     {         int i;     for (i=0;i<3;i++)         scanf("%d,%d,%d,%d;",&v[i].tran_id,&v[i].acc_no,&five[i].dr_cr,&five[i].amount);     for (i=0;i<3;i++)          printf("i %d %d %d %d %d \n",i,v[i].tran_id,5[i].acc_no,5[i].dr_cr,five[i].amount);     }            

if I enter "1,120,0,12000;1,126,1,12000;1,128,2,13000;" I get

1,120,0,12000;1,126,one,12000;1,128,2,13000; i 0 1 120 0 12000 i 1 ane 126 one 12000 i ii 1 128 2 13000  Procedure returned nineteen (0x13)   execution time : five.680 s Printing any key to continue.            

This post has been edited past horace: 30 September 2015 - 08:13 AM

holleranpuring.blogspot.com

Source: https://www.dreamincode.net/forums/topic/381275-reading-a-comma-separated-file-in-c/

0 Response to "How to Delimit Comma While Reading a Text File C++"

ارسال یک نظر

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel