write string to an array

M

MJK

Suppose I have the following function in my program:

void ExtractData(Ind *AM)
{
int i,j;
char str[255];
char c;

FILE *ext=fopen("test.out","r");
//suppose I have N line each with M digits (in here M=5) like: 1 2 2
1 3

for(i=0; i<N; ++i)
{
fscanf(ext,"%s",str); //This line read the whole 5 digits
//I want each of those five digits become an element of
"AM.S[j]" array as below:
for(j=0; j<M; ++j)
{
c=fgetc(ext);
AM.S[j]=int(c);
}
}

The above program is not working. Does any of you have any idea that
how I should write this part of my code?
Thanks,
MJK
 
C

Chris Dollin

MJK said:
Suppose I have the following function in my program:

void ExtractData(Ind *AM)

We don't know what type `Ind` is.
{
int i,j;
char str[255];
char c;

FILE *ext=fopen("test.out","r");

Check that `ext` isn't null.

[Isn't "test.out" a misleading name for an input file?]
//suppose I have N line each with M digits (in here M=5) like: 1 2 2
1 3

With spaces between the digits?
for(i=0; i<N; ++i)
{
fscanf(ext,"%s",str); //This line read the whole 5 digits

Not if they're separated by spaces, it doesn't. Check what the
format spec for `%s` says. You might be interested in the input
function `fgets`. (NOT `gets`.)
//I want each of those five digits become an element of
"AM.S[j]" array as below:
for(j=0; j<M; ++j)
{
c=fgetc(ext);


Um ...

(a) you're reading more characters from `ext` and are ignoring
the characters you've already read.

(b) `fgetc` returns an `int`, not a `char`, so that it has room
to return EOF.
AM.S[j]=int(c);


(a) `int` is a type, not a function: you can't use it like that.

(b) Since we don't know what `Ind` means, we don't know what type
`AM.S[j]` is, so we don't know what to do with the character
you hoped to have read.
}
}

The above program is not working. Does any of you have any idea that
how I should write this part of my code?

Read an entire line in, then go looking for the digits and do with
them as you will.
 
W

Walter Roberson

MJK said:
//suppose I have N line each with M digits (in here M=5) like: 1 2 2 1 3
fscanf(ext,"%s",str); //This line read the whole 5 digits

The %s format does not read an entire line: it skips leading whitespace,
then read the next string of non-whitespace, leaving the terminating
whitespace in the input buffer. Thus, the above statement would read only
1 of the digits, not all five (you show whitespace between the digits
in your example.)

Also, it is generally dangerous to use a %s format by itself,
if you do not have perfect control over the input, as someone might
choose to input a longer input string than there is space to store
in your str variable. Either do not use fscanf() for your reading, or
else put in an explicit length limitation in the format, such as

"%.254s"
//I want each of those five digits become an element of
"AM.S[j]" array as below:
for(j=0; j<M; ++j)
{
c=fgetc(ext);


Your comment on the fscanf() indicates that you believe you have
already read in the entire line (into the buffer str), but here
you are trying to read more from the file. Also, you are not skipping
over whitespace when you use fgetc().
AM.S[j]=int(c);


There is no 'int' function in C, and that would not be the correct
syntax for a type conversion.

If you know that c is a single character representing a numeric
digit, then the short portable way to extract the numeric value
it represents is to use c - '0' (subtract the quoted 0 character
from c). C promises that this will work no matter what character set
you are using, even if the character set is EBCDIC . In the
more general multi-character case such converting the string "42" to the
number 42, there are several choices on how to proceed, with various
tradeoffs.
 
P

pete

MJK said:
Suppose I have the following function in my program:

void ExtractData(Ind *AM)
{
int i,j;
char str[255];
char c;

FILE *ext=fopen("test.out","r");
//suppose I have N line each with M digits (in here M=5) like: 1 2 2
1 3

for(i=0; i<N; ++i)
{
fscanf(ext,"%s",str); //This line read the whole 5 digits
//I want each of those five digits become an element of
"AM.S[j]" array as below:
for(j=0; j<M; ++j)
{
c=fgetc(ext);
AM.S[j]=int(c);
}
}

The above program is not working. Does any of you have any idea that
how I should write this part of my code?
Thanks,
MJK


/* BEGIN test2.c */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define M 5
#define N 11
#define LENGTH 254
#define str(x) # x
#define xstr(x) str(x)

typedef struct {
char S[M];
} Ind;

void ExtractData(Ind *AM);

int main(void)
{
Ind Cool[N];
size_t array, letter;

ExtractData(Cool);
for (array = 0; array != N; ++array) {
for (letter = 0; letter != M; ++letter) {
putchar(Cool[array].S[letter]);
putchar(' ');
}
putchar('\n');
}
return 0;
}

void ExtractData(Ind *AM)
{
int rc, i, j;
char str[LENGTH + 1], *p;
FILE *ext;

ext = fopen("test.out", "r");
if (ext == NULL) {
puts("ext == NULL");
exit(EXIT_FAILURE);
}
/*
** suppose I have N line each with M digits
** (in here M=5) like: 1 2 2 1 3
*/
for (i = 0; N > i; ++i) {
rc = fscanf(ext, "%" xstr(LENGTH) "[^\n]%*[^\n]", str);
if (!feof(ext)) {
getc(ext);
}
if (rc == 0) {
str[0] = '\0';
}
if (rc == EOF) {
puts("rc equals EOF");
exit(EXIT_FAILURE);
}
/*
** This line read the whole 5 digits
** I want each of those five digits become an element of
** "AM.S[j]" array as below:
*/
p = str;
for (j = 0; M > j; ++j) {
while (!isdigit(*p)) {
if (*p == '\0') {
puts("*p == '\0'");
exit(EXIT_FAILURE);
}
++p;
}
AM.S[j] = *p++;
}
}
}

/* END test2.c */
 
P

pete

/* BEGIN test2.c */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define M 5
#define N 11

I used an 11 line test file,
in case you're wondering where that number came from.

1 2 2 1 3
1 2 3 1 3
1 2 5 1 3
1 2 2 1 3
1 9 2 1 3
1 4 2 1 3
1 2 0 1 3
1 2 2 7 3
3 2 2 1 3
1 2 2 1 8
6 2 2 1 3
 
P

pete

pete wrote:
void ExtractData(Ind *AM)
{
int rc, i, j;
char str[LENGTH + 1], *p;
FILE *ext;

ext = fopen("test.out", "r");
if (ext == NULL) {
puts("ext == NULL");
exit(EXIT_FAILURE);
}
/*
** suppose I have N line each with M digits
** (in here M=5) like: 1 2 2 1 3
*/
for (i = 0; N > i; ++i) {
rc = fscanf(ext, "%" xstr(LENGTH) "[^\n]%*[^\n]", str);
if (!feof(ext)) {
getc(ext);
}
if (rc == 0) {
str[0] = '\0';
}
if (rc == EOF) {
puts("rc equals EOF");
exit(EXIT_FAILURE);
}
/*
** This line read the whole 5 digits
** I want each of those five digits become an element of
** "AM.S[j]" array as below:
*/
p = str;
for (j = 0; M > j; ++j) {
while (!isdigit(*p)) {
if (*p == '\0') {
puts("*p == '\0'");
exit(EXIT_FAILURE);
}
++p;
}
AM.S[j] = *p++;
}
}


fclose(ext);
 

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

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top