Solved

Taking integers from txt

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 how to create, access, and change arrays in the C programming language.

911 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

22 Experts available now in Live!

Get 1:1 Help Now