How to read & write data from file to other using our own format
Discuss How to read & write data from file to other using our own format in the C Programming forum on Dev Shed. How to read & write data from file to other using our own format C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
Posts: 11
Time spent in forums: 1 h 44 m 53 sec
Reputation Power: 0
How to read & write data from file to other using our own format
Hiii How to read data from a file in a given format & write to other file using different format based on a condition.
For example the given data as shown below i need to read each colum seperately & write to the file based on range
My file contains data like
Posts: 11
Time spent in forums: 1 h 44 m 53 sec
Reputation Power: 0
If i use fget to read the line then how will it compare my values thats between 3.00 to 4.00.
fget reads the whole line its a string. but i want to check if my 19th column data is in between a range & then print the whole line.how is this.
Could you explain with a simple code or example.
Posts: 5,964
Time spent in forums: 2 Months 3 Weeks 2 Days 12 h 47 m 19 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 4851
Why don't you read your response? Read the line as a string, then parse it with sscanf into the fields, do your compare, and make your decision.
As a matter of fact, you could scan from the line into one variable, in a loop, until you had the 19th, compare, then if your condition was met, write the saved line into the output file. Wouldn't need all those variables.
Posts: 3,398
Time spent in forums: 3 Weeks 5 Days 6 h 48 m 17 sec
Reputation Power: 886
clifford gave you the solution. He told you to read the file using fgets() and to use sscanf to process that string after you've read it. Nobody is going to write the code for you because it's a well known homework assignment and we don't do your homework for you. Now read the manual on the sscanf() function (it's a close relative of fscanf()).
__________________
My worst nightmare was a pointless infinite loop.
Work in progress; don't poke the curmudgeon! http://www.odonahue.com/
Posts: 4,825
Time spent in forums: 1 Month 2 Days 21 h 7 m 38 sec
Reputation Power: 1800
Read my answer more carefully, or I'll be wondering why I bother.
By using sscanf() on a line rather than fscanf() on a file, the scan is limited to the line not the whole file, so it will not skip to the next line and get out of synch when not all fields are present.
sscanf() terminates on encountering the nul terminator that fgets() inserts. fscanf() terminates on end-of-file - the end of the line is no barrier to conversion, that is your problem. Both of course also terminate when all field format specifiers have been used.
Here thier a error in my output just check out i have blank space in column 21 for 6 &7th rows but its taking the data or string from column21 of row 5.Same thing contiues for other rows also like for example 9th rows column21 is blank but now its filled with 8th rows column 21s string.
How to decode this error for getting exact ouput as ahown below
Posts: 5,538
Time spent in forums: 2 Weeks 4 Days 2 h 38 m 46 sec
Reputation Power: 242
For my money I would look at using strtok rather than sscanf. That way you can step through and see exactly what is happening with each variable you extract and store.
It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up
The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw
Posts: 4,825
Time spent in forums: 1 Month 2 Days 21 h 7 m 38 sec
Reputation Power: 1800
You have not checked the return value form sscanf()!
It returns the number of fields successfully converted. For the 'short' records, this will be one fewer than the number of format specifiers, and the variable for the last field will remain un-modified.
Use the value returned by scanf() to determine how many fields to print. For short records you don't want to print the last field.
You could simply clear p to a blank string before sscanf(), or have a conditional to print either 20 or 21 fields. Either way should still check the number of fields for validity (in this case it must be 20 or 21), anything else is a bad record.
Posts: 5,964
Time spent in forums: 2 Months 3 Weeks 2 Days 12 h 47 m 19 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 4851
The idea is that if certain variables are useless, you discard them. This seems obvious to me, but then I'm an ancient azz who doesn't understand how things really work.
Posts: 3,398
Time spent in forums: 3 Weeks 5 Days 6 h 48 m 17 sec
Reputation Power: 886
Hmm... I haven't either. I suspect I meant to agree with you actually, but I don't have the bandwidth at the moment to go through this entire thread and really zero in on what I might have been thinking. Just reading the message without additional context, I don't see anything wrong with the advice you gave.
Sorry Clifford! I really think I just clicked on the wrong radio button. Normally when I disagree with someone, I do say why. I often don't feel the need to comment further when I do agree.