Problem with C program
This is a problem im doing for the ctutorial questions. It asked me to write a program that takes input from the user and removes all the spaces and replaces them with newlines. I did this to solve the problem but im getting one issue.
It displayes the error "Error Reading file for input" this is the error i told it to put if the open command return with a null. problem is with the this part "temp = fopen ("temp.str","w");" in theory acording to my tutorial this should find the file if it exists remove its contents and open it for writeing. problem is thats not what is occuring. its just apending the file not removing the current contents. even if the file does not exist i still get the message. mentiond above when trying to read the file. all and all the program works correctly ? so do i just need to abandon my quest with high level file routines and go low level or am i doing something wrong ? here is the code Code:
/* program takes input from user and strips */ |
What's the getline() function?
|
Quote:
Quote:
But why are you writing the input to a file and then read it back in? Once the input from the user is read into the buffer (the char *typed is the buffer) you can parse it for spaces from that buffer! Why first writing it to a file and then reading it into another buffer before parsing it? The getline() function is an advanced version of fgets(), and does some memory-fiddling if the buffer is not large enough to hold the data read. You are not using or checking for this, and I think you don't understand what its memory-fiddling feature is about (you have a mistake in your code caused by this, see comment below). Also getline() is a GNU-only function (it's not a commonly used function), which means your program will not compiled and run correctly on UNIX-like OS's that have not the GNU libc installed. In other words: it's not portable. I'd recommend fgets() instead of getline(), unless you have special reasons (which you don't in this case). One more thing: When you compile with "-Wall" you get all compiler warnings. This will give you more clues about mistakes. Code:
/* |
Yea seams to me im not totaly grasping the whole sizeof thing yet. Im just learning c and this is the first problem that was semi advanced that i have tried to tackle. The tutorial praises getline that is why i tried to use it instead of other methods. It doesent even mention fgets.
as for why i did not use the buffer im still a little in the fog on buffers. And file postion. I figured i would need to make a file because im using file postion to determine where the next word typed is. Is there a better way to replace a character in a string of undetermined length rather then the model that i have created ?? |
And remove the casts of malloc()'s return value:
Code:
typed = (char *) malloc (SIZE); Code:
typed = malloc (SIZE); I wonder why people insist on casting...must be alot of crappy tutorials out there. I know you have to cast in C++ but in C++ you should use new/delete because malloc/free doesn't understand constructors. |
Yea stupid tutorial never even explained what the (char * ) even was just that it needed to be in front of malloc.
I dident see why i needed to put it there either ??? |
The header and the ; in front of the if statement was my own bad :P and i think i tracked my less on the file problem down. Its less thats messed up not my data file that was created :P
STUPID LESS BUFFER !! |
Quote:
Code:
int main() Quote:
Quote:
Code:
void striprint(char *typed) |
Quote:
"Man malloc" or "grep malloc /usr/include/stdlib.h" will show you that malloc returns a void*, so it must be casted to the destination pointer type, unless it is a void*. Not doing it will trigger a C compiler warning. |
Quote:
Code:
itsme@dreams:~/C$ cat nowarn.c |
LOL this is so much eazier
Code:
void striprint(char *typed) silly me forgetting strings are arrays :tisk: |
Or you could just do it on-the-fly while printing the output:
Code:
{ |
Actually theres a problem with doing this way. What happens is because the size of the buffer is 100 it asumes the string is 100 so it loops untill it gets to 100 rather then stopping at the end of the input. Maybe this is why i dident do it this way?
|
upon setting malloc to 1 i get some very strange results.
|
never mind i wasent minding wehre the } are
it seams to work ok but i wonder if you make malloc too big would it continue to loop untill it got to 100 |
All times are GMT -5. The time now is 12:08 PM. |