Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > write string to an array

Reply
Thread Tools

write string to an array

 
 
MJK
Guest
Posts: n/a
 
      06-06-2007
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[i].S[j]" array as below:
for(j=0; j<M; ++j)
{
c=fgetc(ext);
AM[i].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

 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      06-06-2007
MJK wrote:

> 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[i].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[i].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[i].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.

--
Well-Alined Hedgehog
"Our future looks secure, but it's all out of our hands"
- Magenta, /Man and Machine/

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      06-06-2007
In article <(E-Mail Removed) .com>,
MJK <(E-Mail Removed)> wrote:

> //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[i].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[i].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.
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      06-06-2007
MJK wrote:
>
> 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[i].S[j]" array as below:
> for(j=0; j<M; ++j)
> {
> c=fgetc(ext);
> AM[i].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[i].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[i].S[j] = *p++;
}
}
}

/* END test2.c */

--
pete
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      06-06-2007
pete wrote:
>
> MJK wrote:
> >


> > 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


> /* 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

--
pete
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      06-06-2007
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[i].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[i].S[j] = *p++;
> }
> }


fclose(ext);

> }
>
> /* END test2.c */


--
pete
 
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
To write an array from the text file into an array variable using C amrutha0303 Software 0 08-03-2010 02:57 PM
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
'System.String[]' from its string representation 'String[] Array' =?Utf-8?B?UmFqZXNoIHNvbmk=?= ASP .Net 0 05-04-2006 04:29 PM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM
Need to write a function that adjusts words in a string array Rick C++ 8 11-25-2003 07:04 PM



Advertisments