Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C Programming Language

write string to an array


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

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/

In article <1181164466.894053.266@g37g2000prf.googlegroups.com>,

MJK  <jafari@gmail.com> 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

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

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

    fclose(ext);

> }

> /* END test2.c */

--
pete
Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc