Urgent Strings help!!!

Reply

Join Date: Dec 2009
Posts: 2
Reputation: newcoder777 is an unknown quantity at this point 
Solved Threads: 0
newcoder777 newcoder777 is offline Offline
Newbie Poster

Urgent Strings help!!!

 
0
  #1
Dec 6th, 2009
hi there.
i have 6 long numbers in a text file that i am reading from.
these numbers can be of any length but written in the one of the formats as i have written below.
the numbers are

102,2131,432 //format 1
32,531 //format1
74 //format2
534532.5 //format3
1,500 //format1
3,120,352 //format1

i want the output as
102
32
74
534532
1
3

. i.e. the numbers before the commas or the dots. i want to discard everything else. Any idea how i can get them???

i have the code to read the file and stuff. i just need the code for this bit.
the output is inside the same while loop that i am using to read the file. the loop is...
while(fscanf(MYFILE,"%s", &STOREHERE) == 1)
{
//my code is here
}

i dont want to put my code here because its a very long and complicated one. n i dont want to confuse all of u by telling u d whole thing. please help me in getting just what i have stated above.
Many thanks in advance,
Tim.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 725
Reputation: gerard4143 has a spectacular aura about gerard4143 has a spectacular aura about gerard4143 has a spectacular aura about 
Solved Threads: 91
gerard4143's Avatar
gerard4143 gerard4143 is online now Online
Master Poster
 
-1
  #2
Dec 6th, 2009
I think I understand what you want - try investigating the isdigit() function.
Reply With Quote Quick reply to this message  
Join Date: Dec 2009
Posts: 2
Reputation: newcoder777 is an unknown quantity at this point 
Solved Threads: 0
newcoder777 newcoder777 is offline Offline
Newbie Poster
 
0
  #3
Dec 7th, 2009
Originally Posted by gerard4143 View Post
I think I understand what you want - try investigating the isdigit() function.

as far as i know, the isdigit function will not work for a large number including commas.
for xample, a number like 43,123,321 wont be handled by the function
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 725
Reputation: gerard4143 has a spectacular aura about gerard4143 has a spectacular aura about gerard4143 has a spectacular aura about 
Solved Threads: 91
gerard4143's Avatar
gerard4143 gerard4143 is online now Online
Master Poster
 
0
  #4
Dec 7th, 2009
Originally Posted by newcoder777 View Post
as far as i know, the isdigit function will not work for a large number including commas.
for xample, a number like 43,123,321 wont be handled by the function
The number your referring to is a string of ascii characters...
Last edited by gerard4143; Dec 7th, 2009 at 10:52 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,773
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 308
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
1
  #5
Dec 7th, 2009
#include <stdio.h>

int main()
{
   const char filename[] = "file.txt";
   FILE *file = fopen(filename, "r");
   if ( file )
   {
      char line[BUFSIZ], text[10];
      while ( fgets(line, sizeof line, file) )
      {
         if ( sscanf(line, "%9[^, .]", text) == 1 )
         {
            printf("text = \"%s\"\n", text);
         }
      }
   }
   return 0;
}

/* file.txt
102,2131,432 //format 1
32,531 //format1
74 //format2
534532.5 //format3
1,500 //format1
3,120,352 //format1
*/

/* my output
text = "102"
text = "32"
text = "74"
text = "534532"
text = "1"
text = "3"
*/
Last edited by Dave Sinkula; Dec 7th, 2009 at 11:32 am.
“The essential notion of a socialist society is force.”
— Milton Friedman
Reply With Quote Quick reply to this message  
Join Date: Dec 2009
Posts: 3
Reputation: hichnam is an unknown quantity at this point 
Solved Threads: 0
hichnam hichnam is offline Offline
Newbie Poster
 
0
  #6
Dec 8th, 2009
thanks dave... you provide me a very nice information its really very useful for me.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 419 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC