-
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 :)
-
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.
-
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 :))
-
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.
-
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 =).
-
>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