Solved

Taking integers from txt

Posted on 2006-10-22
4
247 Views
Last Modified: 2011-10-03
we have txt file. like


1 22      33   45            6    4


the space between integers are not constant. and everytime when different txt ( various spaces betment int ) comes, it may change.

i want to take integers from txt file, and put them into ex: k,l,m .... ect variables.


      file=fileopen("input.txt");
      fscanf(a, "%d", &k );
      
      
      printf("%d",k);

this one only prints first integer, how to modify.
0
Comment
Question by:thefirstfbli
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 20 total points
ID: 17785791
Hi thefirstfbli,

char buffer[128];
int temp;
char * endptr, *ptr;

while (fgets(buffer, 128, file))
{
        ptr = buffer;
        while (*ptr)
        {
              temp = strtol (ptr, &endptr,10);
              printf ("number is %d\n",temp);
              temp = endptr;
        }
}

Add error checking as required.

Cheers!
sunnycoder
0
 
LVL 22

Expert Comment

by:grg99
ID: 17786021
Just  keep doing  fscanf's just like you're  doing!


0
 
LVL 2

Accepted Solution

by:
avsrivastava earned 105 total points
ID: 17786287
just as grg99 suggested:

#include<stdio.h>
int main()
{
        int k,l,m,n,o;
        FILE *a=fopen("input.txt", "r");
        fscanf(a, "%d", &k );
        fscanf(a, "%d", &l );
        fscanf(a, "%d", &m );
        fscanf(a, "%d", &n );
        fscanf(a, "%d", &o );

        printf("%d\t",k);
        printf("%d\t",l);
        printf("%d\t",m);
        printf("%d\t",n);
        printf("%d\t\n",o);
}
0
 
LVL 16

Expert Comment

by:MohanKNair
ID: 17786822
char buf[128];
char *a[15];
char *p;
int i=0,j=0;

fgets(buf, 128, file);

p=buf;
while(*p){
if ((j==0) && (*p>='0') && (*p<='9')) {a[i]=p;j=1;i++;}
if ((j==1) && ((*p<'0') || (*p>'9'))) {*p=0;j=0;}
p++;
}

for (j=0;j<i;j++) printf("%s  ", a[i]);
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question