Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Extracting from a file

Reply
Thread Tools

Extracting from a file

 
 
andy.lee23@gmail.com
Guest
Posts: n/a
 
      02-03-2005
Hi, I've been learning C for a couple of weeks and I need some help. I
have a text file and I want to extract integers from it to perform
calculations.

The format of the file is as follows:

3 2
3 5
5 7


I have written code to extract each line and put it into a string, but
I don't know how to convert a string to an integer.

Any help would be appreciated

Thanks in advance.

 
Reply With Quote
 
 
 
 
David Resnick
Guest
Posts: n/a
 
      02-03-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> The format of the file is as follows:
>
> 3 2
> 3 5
> 5 7
>
>
> I have written code to extract each line and put it into a string, but
> I don't know how to convert a string to an integer.
>


Seems like there are two options. For converting a string to an integer
look at strtol. Or you can use sscanf. Here is an example program that
handles this both ways. You should add suitable error checking, this
code is not protected against bad input (e.g. check return of sscanf).
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
const char *str = "3 2";
char *endptr;
int i, j, l, m;

i = strtol(str, &endptr, 10);
j = strtol(endptr, NULL, 10);

sscanf(str, "%d %d",&l,&m);
printf("%d %d %d %d\n", i, j, l, m);
return 0;
}

-David
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
extracting file from the file descriptor akaarj@rediffmail.com C++ 3 06-15-2007 09:16 PM
extracting unique strings from text file Bubbles ASP .Net 0 03-03-2004 06:55 PM
Extracting settings from config file outside 'ConfigurationSettings.AppSettings' ASP .Net 3 02-23-2004 06:06 AM
extracting event logs to a file Gnaneshwar Babu Perl 0 12-31-2003 08:24 AM
possible to read self-extracting zip file? Bomb Diggy Java 17 08-29-2003 08:42 AM



Advertisments