i am trying to read a text file like this and store each number as an integer (without the decimals) and print them

1 3 5.6 3 2.1 4
2 2 3 4.2 1

here is what i have now

#include <stdio.h>
#include <conio.h>


void main()
{
 int i, n=1;
 int n1=0,n2=0,n3=0,n4=0,n5=0,n6=0,n7=0,n8=0,n9=0,n10=0;
 char c[10];
 FILE *f1;
 f1=fopen("textfile.txt","r");
 while(fgets(c,10,f1)!=NULL)
 {
  sscanf(c,"%d %d %d %d %d %d %d %d %d %d", &n1, &n2, &n3, &n4, &n5, &n6, &n7, &n8, &n9, &n10);
  printf("%d %d %d %d %d %d %d %d %d %d\n",n1,n2,n3,n4,n5,n6,n7,n8,n9,n10);
  getch();
 }

but the output i get is always

1 3 5 0 0 0 0 0 0 0
2 2 3 4 0 0 0 0 0 0

its basically ignoring everything else once the sscanf encounters a decimal

Any suggestions??
Thanks

1. main returns int.
2. a 10 character buffer is nowhere near long enough to store 10 integers expressed as a string.

I would suggest
char c[BUFSIZ];
and update the fgets call accordingly.

Checking the return result of sscanf would be a good idea as well.

> its basically ignoring everything else once the sscanf encounters a decimal
Well what do you want to happen?
%d is for ints. If you want decimals, then use %f and adjust the data types.

Thanks for the input.

1. main returns int.
My bad, i almost alyways use int main

2. a 10 character buffer is nowhere near long enough to store 10 integers expressed as a string.

I've tried it with even a 100 character buffer with no difference in results. I will try BUFSIZ also..

I would suggest
char c[BUFSIZ];
and update the fgets call accordingly.

Checking the return result of sscanf would be a good idea as well.

> its basically ignoring everything else once the sscanf encounters a decimal
Well what do you want to happen?
%d is for ints. If you want decimals, then use %f and adjust the data types.

But if I assign a floating number as an integer, won't it implicitly convert it to int and move to the next character? However, it ignores ALL VALUES in that line of text after the decimal, which is what I find weird. I've also tried with floating point with no change in results...

That's how sscanf works. A decimal is illegal for integers so the translation ends. You'll have to reconsider how you read/translate the file.

Also, consider these problems:

see this about void main() You've defined c[] to be 10 characters. How many characters are in the first line for textfile.txt?

And see this about getch()

Well, i think, this might be a simpler way to solve your problem. I took an array of inetegers to be populated.

int main(void)
{
     FILE *fp;
     char buff[100];
     int i = 0;
     int var[100];
 
     fp = fopen("./test.txt","r");

     while(fgets(buff,sizeof(buff),fp)
     {
          for(i=0;i<strlen(buff);i++)
          {
               if(isdigit(buff[i]))
               {
                    var[i] = buff[i] - 48;
                    printf("%d",var[i]);
                }
          }
     }
     
     printf("\n");
     return 0;
}

Hope it helps you out..

try this out i will work i used fgetc, iguess you should know that its interger not some string to store in.

ASCII 48 = 0 and so on....

#include <iostream>

using namespace std;
#define size 10

int main()
{
     int no[size], temp=0, i;
    
     FILE *fp=fopen("textfile.txt","r");
     int ch=fgetc(fp);
     for(i=0; i<size; ch=fgetc(fp))
     {
             if(ch>='0' && ch<='9') //taking only no
                      temp=(temp*10)+(ch-48);
             else if(temp!=0)
             {
                 no[i]=temp;
                 printf("%d ", no[i]);
                 if(ch==EOF) break;
                 i++;
                 temp=0;
             }
     }
     
     system("pause");
     return 0;
}

try this out i will work i used fgetc, iguess you should know that its interger not some string to store in.

What? :confused:

hope you got the answer to your question....if yes than please acknowledge and help us closing the thread...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.