C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-05-2008, 12:38 AM   #1
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,946
Angry newbie question on strings & pointers

How can I create a variable for storing a string of indeterminate length where a pointer won't work? eg,
Code:
int main () {
     char *text;
     gets(text);
     puts(text);
}
will segfault. I'm asking because I want to read a file into one line with a loop, meaning this could be one very long line by the end. But string ops (strcpy, strcat) will not work with char pointers. Do I have to simply declare "text" as a gigantic array (char text[50000]) or is there a more dynamic means?
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is online now   Reply With Quote
Old 08-05-2008, 12:45 AM   #2
Deathray Engineer
 
MacGyver's Avatar
 
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   Reply With Quote
Old 08-05-2008, 12:58 AM   #3
subminimalist
 
MK27's Avatar
 
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   Reply With Quote
Old 08-05-2008, 01:04 AM   #4
Deathray Engineer
 
MacGyver's Avatar
 
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   Reply With Quote
Old 08-05-2008, 01:57 AM   #5
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,946
Arrow alright

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;
}
"linein" is another function but I think the point is clear...thanx all (ps. is there a simpler way at all??)
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is online now   Reply With Quote
Old 08-05-2008, 04:55 AM   #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   Reply With Quote
Old 08-05-2008, 05:50 AM   #7
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Quote:
Originally Posted by MK27 View Post
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:
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:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Tags
character arrays, pointers, strings

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 08:06 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22