![]() |
| | #1 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| Code: int main () {
char *text;
gets(text);
puts(text);
}
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is online now | |
| | #2 |
| Deathray Engineer Join Date: Mar 2007
Posts: 3,211
| Short answer: You can't. Longer answer: Allocate the memory dynamically. Use a buffer to read, and store into a dynamic buffer. Consistently allocate more memory for the dynamic buffer until you read an entire line. Then use the dynamic buffer. This is a complicated approach. As for your statements that strcpy() and strcat() don't work with char pointers, you obviously are quite confused. They work perfectly fine with pointers. In your example with gets() (which shouldn't be used anyway), you have no memory allocated for your char *, so it points to some arbitrary memory location. gets() then attempts to trash wherever your char * points to. Not surprisingly, your OS slaps your program down to size and kills it for illegal memory access. You have to make your pointers point to valid locations. Pointers are just variables that contain addresses of other variables.
__________________ |
| MacGyver is offline | |
| | #3 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| what would a "dynamic buffer" be?
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is online now | |
| | #4 |
| Deathray Engineer Join Date: Mar 2007
Posts: 3,211
| Memory returned from one of the *alloc() functions. You probably want realloc() for such a case.
__________________ |
| MacGyver is offline | |
| | #5 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| I had my fingers crossed that "dynamic" meant something more "automatic", but anyway, for posterity's sake, here's what I worked out re: reading a file into one character array: Code: char *func (char *filename) {
size_t len, tmem = 0;
char *cumul, *line;
FILE *fst = fopen(filename, "r");
if (fst == NULL) puts("!!fopen failed!");
while ((line = linein(fst)) != NULL) {
len = strlen(line);
if (mem = 0) {
mem = len+1;
cumul = (char *)malloc(mem);
strcpy(cumul,line);
}
else {
mem += len;
cumul = (char *)realloc(mem);
strcat(cumul,line);
}
}
return cumul;
}
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is online now | |
| | #6 |
| Registered User Join Date: Jul 2008
Posts: 133
| Didn't test it, but it should work (but double-check it before usage ![]() Code: char *fReader(char *filename)
{
int size=0;
char *str = NULL;
FILE *f = fopen(filename, "r");
if (!f) { puts("Idiot..."); return(NULL); }
// make sure that even on empty file we return valid string (ends with zero):
// (but if file ends with byte 0, we'll have 2 zeros in the end ;)
// (EDIT: remove this line if you want function to return NULL on both fopen() fail AND
// empty file)
str = calloc(1,1);
for (;;)
{
int c = fgetc(f);
if (c == EOF) break;
size++;
str = realloc(str, size+1);
str[size-1] = c;
str[size] = 0;
}
fclose(f);
return(str);
}
Last edited by rasta_freak; 08-05-2008 at 05:45 AM. |
| rasta_freak is offline | |
| | #7 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Nope, sorry. It's the price you pay for working with such a low-level language as C.
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
![]() |
| Tags |
| character arrays, pointers, strings |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| newbie question about strings | gp364481 | C Programming | 11 | 02-28-2009 05:25 PM |
| Newbie question (Pointers) | Tux_Fan | C Programming | 21 | 11-28-2008 12:21 PM |
| C++ newbie question about pointers | lafayette | C++ Programming | 15 | 09-30-2008 12:38 PM |
| pointers | InvariantLoop | C Programming | 13 | 02-04-2005 09:32 AM |
| Pointers Question.....Null Pointers!!!! | incognito | C++ Programming | 5 | 12-28-2001 11:13 PM |