C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 1st, 2009, 11:45 PM
rrevathy25 rrevathy25 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 11 rrevathy25 New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 1 h 44 m 53 sec
Reputation Power: 0
Unhappy 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
Code:
RAY  1678  1  2  2  1  3.00  19.1000  73.2000  10   0 0.00   0 5.00 0.00 0.00   0 2.40 3.10  0
RAO  1684  1  3  0  1  1.00  21.2000  72.9000  10   0 1.00   0 3.70 2.00 0.00   0 0.00 3.70  0 
SIG  1688  5  0  0  0  0.00  24.0000  68.0000   0   0 0.00   0 0.00 0.00 0.00   0 0.00 3.50 0 ccc11
LEE  1688  6 14  0  0  0.00  26.5000  99.9000   0   0 0.00   0 0.00 0.00 0.00   0 6.20 6.20 0 sss3rr
LEE  1699  9  0  0  0  0.00  39.8000  99.7000   0   0 0.00   0 0.00 0.00 0.00   0 4.70 4.70 0 wwww23
MTL  1898  8 11  2  3  4.50  26.6700  95.9000   4   5 2.00   0 0.00 0.00 3.20   0 3.00 3.20 0 sss
MEL  1999  9  9  9  9  1.00  45.8000  89.7000   0   0 0.00   0 0.00 0.00 0.00   0 4.70 4.70 0 wtyu

Now the output i must write is this data which has 3.00 to 4.00 in column 19 to other file.
How to write such data in same format.

i used the program as given below but i am gettinn an error.Help me out .
Code:
void main(){
FILE *fp,*gp;
char x[6],p[20];
float a,b,t,tt,f,u,v,h,j,k,l,n,ff ;
int c,d,q,r,s,e,g,i,m,o;
fp=fopen("abc.dat","r");
gp=fopen("xyz.dat","w");
while(feof(fp)<=0)
{
fscanf(fp,"%s %d %d %d %d %d %f %f %f %d %d %f %d %f %f %f %d %f %f %d %s\n", x, &c, &d, &q, &r, &s, &f, &u, &v, &e, &g, &h, &i, &j, &k, &l, &m, &n, &ff, &o, p);
if(ff>=3.00 && ff<=4.00)
{
printf(gp,"%s %4d %2d %2d %2d %2d %5.2f %8.4f%c %8.4f%c %3d %3d %4.2f %3d %4.2f %4.2f %4.2f %3d %4.2f %4.2f %4d %s\n", x, c, d, q, r,s,f,u,z1,v,z2,e,g,h,i,j,k,l,m,n,ff,o,p);
 }}}

The error this program gives is it prints the output as shown below in the other file:
Code:
1678  1  2  2  1  3.00  19.1000  73.2000  10   0 0.00   0 5.00 0.00 0.00   0 2.40 3.10  0 RAO
1684  1  3  0  1  1.00  21.2000  72.9000  10   0 1.00   0 3.70 2.00 0.00   0 0.00 3.70  0 SIG
SIG  1688  5  0  0  0  0.00  24.0000  68.0000   0   0 0.00   0 0.00 0.00 0.00   0 0.00 3.50 0 ccc11
1898  8 11  2  3  4.50  26.6700  95.9000   4   5 2.00   0 0.00 0.00 3.20   0 3.00 3.20 0 sss MEL

Its taking the next rows first colum as string why is it.
I want to get the correct output as
Code:
RAY  1678  1  2  2  1  3.00  19.1000  73.2000  10   0 0.00   0 5.00 0.00 0.00   0 2.40 3.10  0
RAO  1684  1  3  0  1  1.00  21.2000  72.9000  10   0 1.00   0 3.70 2.00 0.00   0 0.00 3.70  0 
SIG  1688  5  0  0  0  0.00  24.0000  68.0000   0   0 0.00   0 0.00 0.00 0.00   0 0.00 3.50 0 ccc11
MTL  1898  8 11  2  3  4.50  26.6700  95.9000   4   5 2.00   0 0.00 0.00 3.20   0 3.00 3.20 0 sss

Help me out ...........

Reply With Quote
  #2  
Old September 2nd, 2009, 01:22 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,825 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 21 h 7 m 38 sec
Reputation Power: 1800
Not all your lines have a final string. fscanf() uses whitespace as a delimiter and newline is a delimiter. You need to use line oriented I/O.

Use fgets() to read one line as a time, then use sscanf to parse the fields for a single line at a time.

Clifford

Reply With Quote
  #3  
Old September 2nd, 2009, 02:27 AM
rrevathy25 rrevathy25 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 11 rrevathy25 New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 1 h 44 m 53 sec
Reputation Power: 0
Unhappy

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.

Reply With Quote
  #4  
Old September 2nd, 2009, 06:02 AM
sizablegrin's Avatar
sizablegrin sizablegrin is offline
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Jun 2005
Posts: 5,964 sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level) 
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.

Read your responses and think about them.
__________________
Write no code whose complexity leaves you wondering what the hell you did.
Politically Incorrect DaWei on Pointers Grumpy on Exceptions

Reply With Quote
  #5  
Old September 2nd, 2009, 10:50 AM
jwdonahue's Avatar
jwdonahue jwdonahue is offline
Bellevue WA, USA
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 3,398 jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level) 
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/

Reply With Quote
  #6  
Old September 2nd, 2009, 11:25 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,825 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
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.

Clifford

Reply With Quote
  #7  
Old September 2nd, 2009, 10:31 PM
rrevathy25 rrevathy25 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 11 rrevathy25 New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 1 h 44 m 53 sec
Reputation Power: 0
Thanks a lot i got it.....

Reply With Quote
  #8  
Old September 3rd, 2009, 12:34 AM
rrevathy25 rrevathy25 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 11 rrevathy25 New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 1 h 44 m 53 sec
Reputation Power: 0
Question

Hii i am having a error using sscanf & fget
The input file contains data lik as shown below
Code:
RAO  1684  1  3  0  1  1.00  21.2000  72.9000  10   0 1.00   0 3.70 2.00 0.00 0 0.00 3.70  0 
SIG  1688  5  0  0  0  0.00  24.0000  68.0000   0   0 0.00   0 0.00 0.00 0.00 0 0.00 3.50 0 ccc11
LEE  1699  9  0  0  0  0.00  39.8000  99.7000   0   0 0.00   0 0.00 0.00 0.00 0 4.70 4.70 0 wwww23
MTL  1898  8 11  2  3  4.50  26.6700  95.9000   4   5 2.00   0 0.00 0.00 3.20 0 3.00 3.20 0 sss
MEL  1999  9  9  9  9  1.00  45.8000  89.7000   0   0 0.00   0 0.00 0.00 0.00 0 4.70 4.70 0 aaaaa
PDE  2000 12 26 17 43 19.53  37.1860 104.0540  33   0 4.20   0 0.00 0.00  0.00 0 0.00 4.20 0
GU   2001  1  2 16 22 37.10  36.1670  69.0730  33   0 5.10   0 0.00 5.20 0.00 0 0.00 5.20 0    
GS   2002  6  8  9  1 56.90  33.5150  75.6810  59   0 4.20   0 0.00 0.00 0.00 0 0.00 4.20 0 qqqq
GAS  2001  9  5  2  1 56.90  29.2050  75.6000  59   0 4.20   0 0.00 0.00 0.00 0 0.00 4.20 0 
PS   2004  8  8  1  6 50.00  12.5000  70.5550  59   0 4.20   0 0.00 0.00 0.00 0 0.00 4.20 0 rrr23f


The output file which i am getting using sscanf & fget is
as shown below
Code:
RAO  1684  1  3  0  1  1.00  21.2000  72.9000  10   0 1.00   0 3.70 2.00 0.00 0 0.00 3.70  0 
SIG  1688  5  0  0  0  0.00  24.0000  68.0000   0   0 0.00   0 0.00 0.00 0.00 0 0.00 3.50 0 ccc11
LEE  1699  9  0  0  0  0.00  39.8000  99.7000   0   0 0.00   0 0.00 0.00 0.00 0 4.70 4.70 0 wwww23
MTL  1898  8 11  2  3  4.50  26.6700  95.9000   4   5 2.00   0 0.00 0.00 3.20   0 3.00 3.20 0 sss
MEL  1999  9  9  9  9  1.00  45.8000  89.7000   0   0 0.00   0 0.00 0.00 0.00 0 4.70 4.70 0 aaaaa
PDE  2000 12 26 17 43 19.53  37.1860 104.0540  33   0 4.20   0 0.00 0.00  0.00 0 0.00 4.20 0 aaaaa
GU   2001  1  2 16 22 37.10  36.1670  69.0730  33   0 5.10   0 0.00 5.20 0.00 0 0.00 5.20 0 aaaaa   
GS   2002  6  8  9  1 56.90  33.5150  75.6810  59   0 4.20   0 0.00 0.00 0.00 0 0.00 4.20 0 qqqq
GAS  2001  9  5  2  1 56.90  29.2050  75.6000  59   0 4.20   0 0.00 0.00 0.00 0 0.00 4.20 0 qqqq
PS   2004  8  8  1  6 50.00  12.5000  70.5550  59   0 4.20   0 0.00 0.00 0.00 0 0.00 4.20 0 rrr23f


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
Code:
RAO  1684  1  3  0  1  1.00  21.2000  72.9000  10   0 1.00   0 3.70 2.00 0.00 0 0.00 3.70  0 
SIG  1688  5  0  0  0  0.00  24.0000  68.0000   0   0 0.00   0 0.00 0.00 0.00 0 0.00 3.50 0 ccc11
LEE  1699  9  0  0  0  0.00  39.8000  99.7000   0   0 0.00   0 0.00 0.00 0.00 0 4.70 4.70 0 wwww23
MTL  1898  8 11  2  3  4.50  26.6700  95.9000   4   5 2.00   0 0.00 0.00 3.20   0 3.00 3.20 0 sss
MEL  1999  9  9  9  9  1.00  45.8000  89.7000   0   0 0.00   0 0.00 0.00 0.00 0 4.70 4.70 0 aaaaa
PDE  2000 12 26 17 43 19.53  37.1860 104.0540  33   0 4.20   0 0.00 0.00  0.00 0 0.00 4.20 0 
GU   2001  1  2 16 22 37.10  36.1670  69.0730  33   0 5.10   0 0.00 5.20 0.00 0 0.00 5.20 0    
GS   2002  6  8  9  1 56.90  33.5150  75.6810  59   0 4.20   0 0.00 0.00 0.00 0 0.00 4.20 0 qqqq
GAS  2001  9  5  2  1 56.90  29.2050  75.6000  59   0 4.20   0 0.00 0.00 0.00 0 0.00 4.20 0 
PS   2004  8  8  1  6 50.00  12.5000  70.5550  59   0 4.20   0 0.00 0.00 0.00 0 0.00 4.20 0 rrr23f


Reply With Quote
  #9  
Old September 3rd, 2009, 03:27 AM
sizablegrin's Avatar
sizablegrin sizablegrin is offline
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Jun 2005
Posts: 5,964 sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level) 
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
Brighten up. We can't see your code from here unless you post it.

Reply With Quote
  #10  
Old September 3rd, 2009, 05:12 AM
rrevathy25 rrevathy25 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 11 rrevathy25 New User: is a brand new recruit and a unknown entity at this point. 
Time spent in forums: 1 h 44 m 53 sec
Reputation Power: 0
Unhappy

The code which i used is
Code:
 while(fgets(buff,sizeof(buff),fp)!=NULL)
      {
        sscanf(buff,"%s %d %d %d %d %d %f %f %f %d %d %f %d %f %f %f %d %f %f %d %s",x,&c,&d,&q,&r,&s,&f,&u,&v,&e,&g,&h,&i,&j,&k,&l,&m,&n,&ff,&o,p);
    
        fprintf(gp,"%s %4d %2d %2d %2d %2d %5.2f %8.4f%c %8.4f%c %3d %3d %4.2f %3d %4.2f %4.2f %4.2f %3d %4.2f %4.2f %4d %s\n",x,c,d,q,r,s,f,u,z1,v,z2,e,g,h,i,j,k,l,m,n,ff,o,p);
      }

Reply With Quote
  #11  
Old September 3rd, 2009, 06:59 AM
mitakeet's Avatar
mitakeet mitakeet is offline
I'm Baaaaaaack!
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Jul 2003
Location: Maryland
Posts: 5,538 mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level)mitakeet User rank is Captain (20000 - 30000 Reputation Level) 
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.
__________________

My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
Free code: http://sol-biotech.com/code/.
Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

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

Reply With Quote
  #12  
Old September 3rd, 2009, 07:05 AM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,825 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
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.

Clifford
Comments on this post
jwdonahue disagrees!

Reply With Quote
  #13  
Old September 5th, 2009, 06:08 PM
clifford's Avatar
clifford clifford is offline
Contributing User
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Aug 2003
Location: UK
Posts: 4,825 clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level)clifford User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 21 h 7 m 38 sec
Reputation Power: 1800
Quote:
jwdonahue disagrees!
Care to elaborate? I have no idea what you are disagreeing with.

Reply With Quote
  #14  
Old September 5th, 2009, 06:30 PM
sizablegrin's Avatar
sizablegrin sizablegrin is offline
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: Jun 2005
Posts: 5,964 sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level)sizablegrin User rank is General 58th Grade (Above 100000 Reputation Level) 
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.

Reply With Quote
  #15  
Old September 7th, 2009, 03:51 AM
jwdonahue's Avatar
jwdonahue jwdonahue is offline
Bellevue WA, USA
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: May 2004
Location: Bellevue Washington, USA
Posts: 3,398 jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level)jwdonahue User rank is Lieutenant General (80000 - 90000 Reputation Level) 
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.
Comments on this post
clifford agrees: At least you did not try to add rep points! . I once did the same to Scorpians4ever, so cannot
complain.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > How to read & write data from file to other using our own format

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap