Thread: Newbie question

  1. #1
    Unregistered
    Guest

    Post Newbie question

    Hi, recently I've started learning to program in C

    I stole "A crash course in C" from a friend ages ago and decided i better put it to use.

    I'm trying to put together a little program that will read text line by line from a file, altering each line in some way( add in a char somewhere/ remove the 5 characters at the start of each line etc.) and then output it line by line to a seperate file and i've hit a bit of a brick wall.

    I can load a line of text into a char. pointer with Fgets(), and i can write it out to a file with Fputs() the problem is whenever i alter the the char pointer between inputing the text to it, and outputing it to the file, i get a crash when running the program.

    Hmm.. i havent been very clear, i'll try again

    Basically after declaring two file pointers, I have these lines:

    while (!feof(infile))
    {
    fgets(buffer, 1000, infile);
    fputs(buffer, oufile);
    }

    buffer being the char pointer, and infile and oufile being the file pointers.

    Thing is, i want to alter whats in buffer but whenever i put anything here:

    while (!feof(infile))
    {
    fgets(buffer, 1000, infile);
    xxxxxxxxxx
    fputs(buffer, oufile);
    }

    i get a crash, infact what i find REALLY weird is that if i don't have the loop command in, IE just:

    fgets(buffer, 1000, infile);
    fputs(buffer, oufile);

    I also get a crash.


    Wondering if anyone could explain what I'm doing wrong, thanks in advance

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    42
    try doing:


    Code:
    #include <stdio.h>
    #define MAXSIZE 1000
    
    int main (int argc, char **argv) {
         char buffer[MAXSIZE]
         FILE *infile, outfile;
    
         infile = fopen("test.txt", "r");
         outfile = fopen("writefile.txt", "w");
    
         fgets(buffer, MAXSIZE, infile);
         fputs(buffer, outfile);
         
         fclose(infile);
         fclose(outfile);
    
         return 0;
    }
    You're error is probably because you're using a char * data type and when you try to change the value you haven't declared enough memory to hold the new length.
    http://www.KBeutler.com

  3. #3
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,403
    Thanks very much, you were right, I had buffer defined as a pointer, now that its an array, I have no problem. (still coming to terms with pointers/arrays )

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's an easy difference:

    An array has space allocated for each of whatever its cells are.
    A pointer by default allocates no memory. You have to do this on your own.

    However, you can some-what treat the name of an array as a pointer to its first element.

    int array[30];

    void myfun( int *a );

    Call this:

    myfun( array );

    Now, the difference is this:

    If you're using the name of an array as a pointer, you cannot use
    assignment operators to assign it TO something. You can only
    assign TO IT. Example:

    int *ptr, x;

    ptr = array; /* valid - make ptr point to the first element of 'array' */
    array = ptr; /* invalid. 'array' will always point to its cells. */

    ptr = &x; /* valid */
    array = &; /* invalid as per above */

    *array = x; /* valid - make the contents of array[0] == x */


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

  5. #5
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,403
    Thanks for the tip, I was puzzled because you can do this:

    char *a = "String";
    printf("%s", a)

    I thought that because the above works, I could basically use pointers instead of an arrays..... seems i was wrong =).

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I could basically use pointers instead of an arrays..... seems i was wrong
    Care to guess how often that mistake is made by both learners and more experienced programmers? Despite common knowledge, pointers and arrays are NOT the same.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 04:43 AM
  2. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:45 PM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 04:38 PM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 04:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 09:50 AM