-
segmentation fault
I am trying to read the contents of a file into a char array and then print that array and so far it actually works but it not only prints the array but also "Segmentation fault. (core dumped)". Here is the code I am using.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
if (argc != 3){
printf("Usage: %s <input filename> <file to write to>\n", argv[0]);
}
else {
FILE *ifile = fopen(argv[1], "r");
FILE *ofile = fopen(argv[2], "w");
if (!ifile){
printf("Error: Could not open input file \n");
}
else {
if(!ofile){
printf("Error: Could not write to file %s", argv[2]);
}
else {
int length = 0;
int i = 0;
while(fgetc (ifile) != EOF){
length++;
}
char input[] = "";
rewind(ifile);
for(i = 0; i < length; i++){
input[i] = fgetc(ifile);
}
printf("%s",input);
}
}
}
return 0;
}
I am guessing it does not work because I didn't initialize the length of the input[] array correctly, but how would I go about doing that?
-
You have have
so automatically, the length is computed by the compiler to preserve enough space to fit what is assigned to it, thus ""
What you need is
Code:
char input[length] = "";
If would suggest you to make sure that length is a positive number. You could do that after line 22.
-
You're right, your input array lacks a length, and lacks the code to place a \0 at the end of it.
If your compiler supports variable length arrays, you can do this
char input[length+1];
-
ok so i tried
Code:
char input[length] = "";
but i am getting a compiler error:
Code:
:23:26: error: variable-sized object ‘input’ may not be initialized
-
Salem explained the problem, but you posted the same time so I guess you didn't notice it :)
You have to dynamically allocate your memory, or set from start a constant number that is logical for your operation ( not too small, not too big :) )
-
i also tried length+1 but that doesnt work either
so do i have to allocate memory? or is there a way to enable variable lengths (i am using gcc)
-
length + 1 was meant for the terminating null. You have to remember this for your dynamic allocation too!
I provided you two options... Not one :)
If you interesting read here for the gcc
-
what option would i have to use for gcc though? i read it but it says nothing about variable size
-
-
nope, still doesn't work. i now tried every version of c (c99/c90/c89/etc) and it doesnt work with any of them it is still giving me the error
Code:
main.c: In function ‘int main(int, char**)’:main.c:23:30: error: variable-sized object ‘input’ may not be initialized
when using
Code:
char input[length + 1] = "";
and when i am using
Code:
char input[length + 1];
it gives me this:
Code:
/tmp/ccEoODxU.o:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'collect2: ld returned 1 exit status
i also tried using the flag but that changed nothing
what am i doing wrong
-
Go to post number 5.
(If i were you I would do that. It is not such a big deal ;) )
-
> __gxx_personality_v0
What is your command line for compiling?
Because this suggests you're using g++ to compile your C program.
-
but how would i dynamically allocate memory? sorry, i am kind of new to all this
-
Code:
/tmp/ccEoODxU.o:(.eh_frame+0x13): undefined reference to `__gxx_personality_v0'collect2: ld returned 1 exit status
Are you compiling this using g++ instead of gcc? I think that's the cause of this error.
-