c file reading

S

stewart_bristol

can someone direct me to some compilable example code which reads in
floating points or integers from a tab delimited file (either to
variables or arrays) please? this is driving me mad today

thanks

stew
 
R

Robert Gamble

can someone direct me to some compilable example code which reads in
floating points or integers from a tab delimited file (either to
variables or arrays) please? this is driving me mad today

What do you have so far?

Here is what I came up with, it reads values into a double and then
prints out the values:

#include <stdio.h>

int main (void) {
double d;
int i;
/* Look for a number, store in d, quit on EOF */
while ((i = scanf("%lf", &d)) != EOF)
if (i == 1) /* Successful conversion */
printf("Read %f\n", d);
else
scanf("%*s"),puts("Bad input"); /* Get rid of next word and try
again */
return 0;
}

If you are stuck with the storing into an array part or trying to be
able to ready both floats and integers and differentiate between the
two this won't help much. Show us what you have and be more specific
about what you are trying to accomplish and we can be of more
assistance.

Robert Gamble
 
S

stewart_bristol

Robert said:
What do you have so far?

Here is what I came up with, it reads values into a double and then
prints out the values:

#include <stdio.h>

int main (void) {
double d;
int i;
/* Look for a number, store in d, quit on EOF */
while ((i = scanf("%lf", &d)) != EOF)
if (i == 1) /* Successful conversion */
printf("Read %f\n", d);
else
scanf("%*s"),puts("Bad input"); /* Get rid of next word and try
again */
return 0;
}

If you are stuck with the storing into an array part or trying to be
able to ready both floats and integers and differentiate between the
two this won't help much. Show us what you have and be more specific
about what you are trying to accomplish and we can be of more
assistance.

Robert Gamble

thanks Robert

it is really the file opeing and referencing in the scanf. your code
looks ok, but I need the file open and just get confused with the
arguments in
FILE *stream, *fopen();

and

stream = fopen("myfile.dat","r");

stew
 
R

Robert Gamble

thanks Robert

it is really the file opeing and referencing in the scanf. your code
looks ok, but I need the file open and just get confused with the
arguments in
FILE *stream, *fopen();

and

stream = fopen("myfile.dat","r");

Here is a version that will open the files provided on the commandline
one at a time and process them:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

int main (int argc, char *argv[]) {
double d;
int i, j;
FILE *fp;

if (argc < 2) {
fprintf(stderr, "No filename provided\n");
return EXIT_FAILURE;
}

for (j = 1; j < argc; j++) {
errno = 0;
fp = fopen(argv[j], "r");
if (!fp) {
fprintf(stderr, "Could not open %s: %s\n", argv[j], errno ?
strerror(errno) : "Unknown reason");
continue;
}
while ((i = fscanf(fp, "%lf", &d)) != EOF)
if (i == 1)
printf("Read %f\n", d);
else
fscanf(fp, "%*s"),puts("Bad input");
}
return 0;
}

It's pretty straight-forward but if you have any questions feel free to
ask. As far as getting confused about function prototypes, etc., did
your implementation come with any documentation? If not then it might
serve you well to pick up a good reference on C programming, I
recommend "C: A Reference Manual" by Harbison and Steele.

Robert Gamble
 
P

pemo

can someone direct me to some compilable example code which reads in
floating points or integers from a tab delimited file (either to
variables or arrays) please? this is driving me mad today

Here's a /knock-up/ ... no doubt someone will soon be along to say why it's
either crap, or could be done in a one liner etc.


The input file was [the spaces were tab characters of course!]:

0.123 3.143 6.4 0 333.44 232.454 34334.4 232 10



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main(void)
{
FILE * in = NULL;

if((in = fopen("c:\\test.txt", "rb")) != NULL)
{
long tabCount = 0;
long allCount = 0;
long l = 0;

char c;

char * p = NULL; // For strtok().
char * buffer = NULL; // Holds the inout file.
double * dbs = NULL; // Array of doubles - from the file.

// Read file - store size, and number of tabs seen.
//
while((c = fgetc(in)) != EOF)
{
if(c == '\t')
{
tabCount++;
}

allCount++;
}

rewind(in);

// Alloc space for tab-seperated double values.
//
dbs = malloc(sizeof(double) * ++tabCount);

// Alloc space for the whole file.
//
buffer = calloc(allCount + 1, 1);

// Fill and terminate the buffer from the file.
//
fread(buffer, 1, allCount, in);
//
buffer[allCount] = '\0';

fclose(in);

// Parse for tabs.
//
p = strtok(buffer, "\t");

while(p)
{
// Convert each toekn returned by strtok().
//
dbs[l++] = atof(p);

p = strtok(NULL, "\t");
}

// Done.

// Dump doubles array for checking.
//
while(l)
{
printf("%lf\n", dbs[--l]);
}

free(buffer);
free(dbs);
}

return 0;
}
 
S

stewart_bristol

Robert said:
thanks Robert

it is really the file opeing and referencing in the scanf. your code
looks ok, but I need the file open and just get confused with the
arguments in
FILE *stream, *fopen();

and

stream = fopen("myfile.dat","r");

Here is a version that will open the files provided on the commandline
one at a time and process them:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

int main (int argc, char *argv[]) {
double d;
int i, j;
FILE *fp;

if (argc < 2) {
fprintf(stderr, "No filename provided\n");
return EXIT_FAILURE;
}

for (j = 1; j < argc; j++) {
errno = 0;
fp = fopen(argv[j], "r");
if (!fp) {
fprintf(stderr, "Could not open %s: %s\n", argv[j], errno ?
strerror(errno) : "Unknown reason");
continue;
}
while ((i = fscanf(fp, "%lf", &d)) != EOF)
if (i == 1)
printf("Read %f\n", d);
else
fscanf(fp, "%*s"),puts("Bad input");
}
return 0;
}

It's pretty straight-forward but if you have any questions feel free to
ask. As far as getting confused about function prototypes, etc., did
your implementation come with any documentation? If not then it might
serve you well to pick up a good reference on C programming, I
recommend "C: A Reference Manual" by Harbison and Steele.

thanks very much for your help. i should spend some time starting
again on C from the beginning (it has been 5 years or so). i tend to
rush things a bit without doing the groundwork in the hope i will learn
as i go along. and i usually program by cut and pasting when it comes
to fortran, c and java without necessarily having a deeper
understanding. anyway, that compiles and runs ok, and i may just take
you up on your advice for the book (I had K&R many years ago).

have a nice weekend

stew
 
R

Robert Gamble

Robert said:
thanks Robert

it is really the file opeing and referencing in the scanf. your code
looks ok, but I need the file open and just get confused with the
arguments in
FILE *stream, *fopen();

and

stream = fopen("myfile.dat","r");

Here is a version that will open the files provided on the commandline
one at a time and process them:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

int main (int argc, char *argv[]) {
double d;
int i, j;
FILE *fp;

if (argc < 2) {
fprintf(stderr, "No filename provided\n");
return EXIT_FAILURE;
}

for (j = 1; j < argc; j++) {
errno = 0;
fp = fopen(argv[j], "r");
if (!fp) {
fprintf(stderr, "Could not open %s: %s\n", argv[j], errno ?
strerror(errno) : "Unknown reason");
continue;
}
while ((i = fscanf(fp, "%lf", &d)) != EOF)
if (i == 1)
printf("Read %f\n", d);
else
fscanf(fp, "%*s"),puts("Bad input");

Forgot to close the file here:

fclose(fp);

Robert Gamble
 
K

Keith Thompson

it is really the file opeing and referencing in the scanf. your code
looks ok, but I need the file open and just get confused with the
arguments in
FILE *stream, *fopen();

and

stream = fopen("myfile.dat","r");

I'm not quite sure what you mean here, but if the line
FILE *stream, *fopen();
is part of your code, it shouldn't be. You don't need to declare
fopen yourself; just do a "#include <stdio.h>" and use the prototype
from the header (which is guaranteed to ce correct).
 
D

Dave Thompson

On 17 Mar 2006 08:37:17 -0800, (e-mail address removed) wrote:
thanks very much for your help. i should spend some time starting
again on C from the beginning (it has been 5 years or so). i tend to
rush things a bit without doing the groundwork in the hope i will learn
as i go along. and i usually program by cut and pasting when it comes
to fortran, c and java without necessarily having a deeper
understanding. anyway, that compiles and runs ok, and i may just take
you up on your advice for the book (I had K&R many years ago).
Let me encourage you not to do that. More than most other languages,
probably all the ones you are likely to come across nowadays, C can
punish you for copying or otherwise writing code you don't understand.

In most other programming languages commonly available, most of the
mistakes you are likely to make by accident or misunderstanding will
either be caught outright or will only (!) produce a wrong output.
(Except assembler, the only exception is the 'old' part of Fortran,
which you mention, but the 'newer' parts do offer this safety. Well,
and the roughly C part of C++, but I assumed that was obvious.)

C is often likened metaphorically to a sharp knife, even a surgeon's
scalpel -- you can do impressive and even wonderful things with that
if you know how to use it, but can easily injure yourself or others if
you don't. With the types of computers you are likely to be using this
injury will be only metaphorical -- destroyed or corrupted data, or
lost/wasted time -- but still unpleasant; and in hobby or toy
programming solely for yourself it will probably be you and not
others; but these are still worth avoiding.


- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top