Reading a CSV file
-
marcdev wrote: Use strtok[^] Are you sure that strtok reads a file?? :~:confused::laugh:
-
Hi There may be other more elegant ways, but unless you are reading millions of entries, this works just fine. The
char
variable is read and then ignored, you only store or use the datx as you wish.char separator; ifstream FileIn("C:\\SampleData\\divedata.dat"); FileIn >> dat1 >> separator >> dat2 >> separator >> dat3 >> separator >> dat4;
Good luck -
Hi There may be other more elegant ways, but unless you are reading millions of entries, this works just fine. The
char
variable is read and then ignored, you only store or use the datx as you wish.char separator; ifstream FileIn("C:\\SampleData\\divedata.dat"); FileIn >> dat1 >> separator >> dat2 >> separator >> dat3 >> separator >> dat4;
Good luckknapak wrote: here may be other more elegant ways, but unless you are reading millions of entries, this works just fine. The char variable is read and then ignored, you only store or use the datx as you wish. char separator; ifstream FileIn("C:\\SampleData\\divedata.dat"); FileIn >> dat1 >> separator >> dat2 >> separator >> dat3 >> separator >> dat4; This can hardly work! Good luck Agreed :|
-
knapak wrote: here may be other more elegant ways, but unless you are reading millions of entries, this works just fine. The char variable is read and then ignored, you only store or use the datx as you wish. char separator; ifstream FileIn("C:\\SampleData\\divedata.dat"); FileIn >> dat1 >> separator >> dat2 >> separator >> dat3 >> separator >> dat4; This can hardly work! Good luck Agreed :|
It would be more useful if you could explain why this can hardly work and provide examples of the alternatives. I've used it extensively and had no problems at all, but if there's a good reason to do something else we would be very happy to oblige. Otherwise is just empty discourse. Every time you assign a value to a variable, it takes the new value. If the variable is not used at all, given it is a single char variable, virtually no memory is wasted... why wouldn't it work?:wtf:
-
It would be more useful if you could explain why this can hardly work and provide examples of the alternatives. I've used it extensively and had no problems at all, but if there's a good reason to do something else we would be very happy to oblige. Otherwise is just empty discourse. Every time you assign a value to a variable, it takes the new value. If the variable is not used at all, given it is a single char variable, virtually no memory is wasted... why wouldn't it work?:wtf:
knapak wrote: It would be more useful if you could explain why this can hardly work Try this line with your code:
This,is,a comma seperated,line
-
marcdev wrote: Use strtok[^] Are you sure that strtok reads a file?? :~:confused::laugh:
Obviously not, but the question is not how to read a file ... Maybe you need that I was more specific: 1st. Open the file. 2nd. Read each line putting it in a string. 3rd. Process the string using strtok to separate it by commas. Is strtok useful now ? ;P Marc Soleda. ... she said you are the perfect stranger she said baby let's keep it like this... Tunnel of Love, Dire Straits.
-
knapak wrote: It would be more useful if you could explain why this can hardly work Try this line with your code:
This,is,a comma seperated,line
Fine, if you are mostly writing programs that deal with strings of characters, I can see the problem. However, it seems that were are here talking about reading ONLY numeric data, in which case my approach won't fail and IS SIMPLER. Nevertheless, if you think your solution is more "elegant" why don't you provide a more comprehensive response, something beyond "use this", with clear sample code. Cheers
-
Fine, if you are mostly writing programs that deal with strings of characters, I can see the problem. However, it seems that were are here talking about reading ONLY numeric data, in which case my approach won't fail and IS SIMPLER. Nevertheless, if you think your solution is more "elegant" why don't you provide a more comprehensive response, something beyond "use this", with clear sample code. Cheers
knapak wrote: Nevertheless, if you think your solution is more "elegant" why don't you provide a more comprehensive response, something beyond "use this", with clear sample code. Why not just look at (and re-use) something others have already done, at CodeProject or elsewhere? XTokenString[^] string tokenize[^]