Solved

Taking integers from txt

Posted on 2006-10-22
4
241 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
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now