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
Solved

Taking integers from txt

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I install gcc 4.8.4 on a Linux Ubuntu 14.04 machine? 5 1,946
Problem in finding output of a program 11 106
Arduino EDI - Programming language 3 103
C Programming - If Statement 8 82
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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…
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.

856 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