0

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.

4
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by hichnam
Featured Replies
  • [CODE]#include <stdio.h> int main() { const char filename[] = "file.txt"; FILE *file = fopen(filename, "r"); if ( file ) { char line[BUFSIZ], text[10]; [COLOR="Red"]while ( fgets(line, sizeof line, file) )[/COLOR] { [COLOR="Red"]if ( sscanf(line, "%9[^, .]", text) == 1 )[/COLOR] { printf("text = \"%s\"\n", text); } } } return 0; … Read More

0

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

0

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...

Edited by gerard4143: n/a

1
#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"
*/

Edited by Dave Sinkula: n/a

0

thanks dave... you provide me a very nice information its really very useful for me.

This article has been dead for over six months. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.