write string to an array

Discussion in 'C Programming' started by MJK, Jun 6, 2007.

  1. MJK

    MJK Guest

    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
    MJK, Jun 6, 2007
    #1
    1. Advertising

  2. MJK

    Chris Dollin Guest

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

    --
    Well-Alined Hedgehog
    "Our future looks secure, but it's all out of our hands"
    - Magenta, /Man and Machine/
    Chris Dollin, Jun 6, 2007
    #2
    1. Advertising

  3. In article <>,
    MJK <> 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.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.
    --
    I was very young in those days, but I was also rather dim.
    -- Christopher Priest
    Walter Roberson, Jun 6, 2007
    #3
  4. MJK

    pete Guest

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

    --
    pete
    pete, Jun 6, 2007
    #4
  5. MJK

    pete Guest

    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
    pete, Jun 6, 2007
    #5
  6. MJK

    pete Guest

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

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


    --
    pete
    pete, Jun 6, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UmFqZXNoIHNvbmk=?=

    'System.String[]' from its string representation 'String[] Array'

    =?Utf-8?B?UmFqZXNoIHNvbmk=?=, May 4, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    1,801
    =?Utf-8?B?UmFqZXNoIHNvbmk=?=
    May 4, 2006
  2. Rick
    Replies:
    8
    Views:
    494
  3. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    488
    David RF
    Sep 3, 2009
  4. Tom
    Replies:
    3
    Views:
    207
    salsablr
    Dec 20, 2004
  5. Tuan  Bui
    Replies:
    14
    Views:
    472
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page