Thanks a lot, that really clears it up for me.
Thanks a lot, that really clears it up for me.
This is my signature. Remind me to change it.
I have a little problem with this again... What I'm trying to do is have:
Now, if I wanted to scan in 20 characters into string I could use: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);
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.
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.
-PreludeCode:/*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*/
Last edited by Prelude; 12-05-2001 at 06:08 PM.
My best code is written with the delete key.
Why isn't anyone putting an ampersand preceding the variable in scanf()
ex:
is it not necessary?Code:scanf("%i", &var);
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.
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
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:
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.Code:fread(struct_ptr, sizeof(struct), 1, 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.