Thread: how to make arrays s->name store string ?

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    22

    how to make arrays s->name store string ?

    i got a code something like this
    scanf("%c",&s->name);
    and i want to input string ..
    but i change the %c to %s it error when running

    and
    i want to strore all inside the stulist.name and others into text file by one line

    (fprintf(in"%c",stulist );
    the command above just store all the char into text file but no newline....

    but i dun want to write one line by one line as below

    FILE *in;
    in=fopen("sample.txt","a+");
    fprintf(in,"%c",stulist.name);

    then how ?..............
    ______________________
    Wut ?

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    That's because when you are writing to a string, the name of the string (array) is it. In other words, when you write the name of the string, that is shorthand for the address. So, therefore, you don't need the address of operator (&). Take it out and see what happens.

    --Garfield
    1978 Silver Anniversary Corvette

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    22

    10s

    10s garfield
    ______________________
    Wut ?

  4. #4
    Unregistered
    Guest
    Actually, the & operator says that the following variable is an address. Since the name of an array is a pointer to the address of the first cell then it really doesn't matter if you put the & operator in front or not, though it's best if you don't so that readers can be sure it's an array.

    >scanf("%c",&s->name);
    I think that
    fgets(s->name, sizeof(s->name), stdin);
    would work better for inputting a string

    *pointer

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    22

    but why fgets......

    why fgets will also store my "newline" also
    when i enter for the data i type
    it also store the enter also .......
    then how ?
    ______________________
    Wut ?

  6. #6
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584

    Re: 10s

    Originally posted by Adisonz
    10s garfield
    What??? What does that mean?
    1978 Silver Anniversary Corvette

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    22
    thanks garfield
    ______________________
    Wut ?

  8. #8
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    No problem.
    1978 Silver Anniversary Corvette

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    26
    If you don't like the newline at the end of your fgets() aquired strings here is a function to handle them

    int remNL(char *s)
    {
    char *p;

    p=strchr(s, '\n');
    if(p==NULL)
    {
    return 1;
    }
    *p='\0';

    return 0;
    }

    example

    fgets(myarr, sizeof myarr, stdin);
    remNL(myarr);

    as you can see remNL() returns 1 if there is no newline character in the string, so you can check if you got the whole line of input with this function too... also I would only advise using sizeof myarr if you are using an array, because sizeof myarr won't work to well if myarr is a pointer, which it will be even if you send your array to a function with the following syntax

    void myfunction(char myarr[]) //myarr is actually a pointer
    one fish two fish
    red fish blue fish

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string arrays
    By Molokai in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2007, 11:21 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. string arrays
    By Raison in forum C Programming
    Replies: 27
    Last Post: 10-02-2003, 06:27 PM