Thread: incredibly simple problem (I think)

  1. #16
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597
    Thanks a lot, that really clears it up for me.
    This is my signature. Remind me to change it.

  2. #17
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597
    I have a little problem with this again... What I'm trying to do is have:

    Code:
    #define MAX_LENGTH 20
    
    int main (void)
    {
    	string[MAX_LENGTH];
    
    	scanf("%[^\n]", string);               // or the line below
    	fgets(string, sizeof(string), stdin);
    
    return (0);
    Now, if I wanted to scan in 20 characters into string I could use:
    scanf("%20[^\n]", string);
    but the '20' wouldn't change when I use the #define...

    So I tried to use the fgets(string, sizeof(string), stdin);, but that scans in the '\n' at the end of the string... is it possible to either modify the scanf so that the '20' varies with the #define, or to change the fgets so that it doesn't scan in the '\n' at the end?

    Please help me with this one
    This is my signature. Remind me to change it.

  3. #18
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The following three snippets are possible solutions to your problem. The scanf solution is a cleaner version of what you had before, but it doesn't check for a newline. The getchar solution is probably the most common, and the fgets is most likely what you want.
    Code:
    /*scanf*/
    scanf("%20c", array);
    /*read 20 characters into an array*/
    
    /*getchar*/
    while((array[i++] = getchar()) != '\n' && i < MAX_SIZE)
    /*read MAX_SIZE characters into an array or until \n is reached*/
    
    /*fgets*/
    fgets(array, MAX_SIZE * sizeof(char), stdin);
    /*read MAX_SIZE characters into an array, stop at \n or end of array*/
    -Prelude
    Last edited by Prelude; 12-05-2001 at 06:08 PM.
    My best code is written with the delete key.

  4. #19
    Registered User
    Join Date
    Aug 2001
    Posts
    35
    Why isn't anyone putting an ampersand preceding the variable in scanf()

    ex:
    Code:
    scanf("%i", &var);
    is it not necessary?

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You use the & when you need the address of a non-pointer variable. If you're scanning into a pointer/array, you do not need the &. Recall that the name of an array is the same thing as a pointer.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #21
    Registered User breed's Avatar
    Join Date
    Oct 2001
    Posts
    91
    Reading all the above replies,
    I have posed a question that was answered by this forum, now I'm going to have to re-question.
    Iv'e wrote a prog where a structure is read from a disk file, the reading of the records code goes like this

    while ( fscanf("%6s %1d %4d %4d %5.1f %5.1f", num_ref, t_code, qty, weight, buy_price, sale_price) !=EOF);

    Each record that is read is terminated with a (\n),
    now do you rekon this will have a better result if it went like this.

    while ( fgets("%6s %1d %4d %4d %5.1f %5.1f", num_ref, t_code, qty, weight, buy_price, sale_price) !=EOF);

    ?????

    Before you judge a man, walk a mile in his
    shoes. After that, who cares.. He's a mile away and you've got
    his shoes.
    ************William Connoly

  7. #22
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    If your using structures I would suggest using a binary file to store them, then you can use fread to read all the structure in one go and do away with all the newlines:
    Code:
    fread(struct_ptr, sizeof(struct), 1, fp);
    This will read 1 structure into the structure pointed to by struct_ptr, the sizeof is used to tell the function how many bytes to read and it is read from fp.
    By altering the value 1 you can specify however many you want to read, if you want to read a hundred replace 1 with 100.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 09:10 PM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM