Solved

Taking integers from txt

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

Technology Partners: 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

Suggested Solutions

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

730 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