944,049 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 576
  • C RSS
Dec 7th, 2009
0

Urgent Strings help!!!

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
newcoder777 is offline Offline
11 posts
since Dec 2009
Dec 7th, 2009
-1
Re: Urgent Strings help!!!
I think I understand what you want - try investigating the isdigit() function.
Reputation Points: 499
Solved Threads: 367
Postaholic
gerard4143 is offline Offline
2,197 posts
since Jan 2008
Dec 7th, 2009
0
Re: Urgent Strings help!!!
Click to Expand / Collapse  Quote originally posted by gerard4143 ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
newcoder777 is offline Offline
11 posts
since Dec 2009
Dec 7th, 2009
0
Re: Urgent Strings help!!!
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 11:52 am.
Reputation Points: 499
Solved Threads: 367
Postaholic
gerard4143 is offline Offline
2,197 posts
since Jan 2008
Dec 7th, 2009
1
Re: Urgent Strings help!!!
#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 12:32 pm.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 8th, 2009
0
Re: Urgent Strings help!!!
thanks dave... you provide me a very nice information its really very useful for me.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hichnam is offline Offline
3 posts
since Dec 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: print the fields of a structure
Next Thread in C Forum Timeline: trie





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC