|
|
|
Writing to file in C
|
Original Message
|
Name: the_merv
Date: June 28, 2004 at 17:48:18 Pacific
Subject: Writing to file in COS: WINXP SP1CPU/Ram: 3.8ghz:1gb |
Comment: I am writing an application in c and need to write a string to a text file. It works perfectly when the value of the string is assigned in the source(i.e char somestring[] = "hello, world")but when i want the value of the string to be assigned from another string taken user input("gets()") it only writes the 1st letter of the string and some random characters. This leads me to believe that the "*str1 = *str2" assigment changes the string to some format that cannot be written properly. Any suggestions would be useful. Here is the source code representing the part of the application: "#include <stdio.h> int main () { FILE * pFile; char str[10]; char buffer[10]; gets(str); *buffer = *str; pFile = fopen ("myfile.txt" , "w"); fwrite (buffer , 1 , 10, pFile); fclose (pFile); return 0; }" in the program i input "hi" and the file written contains this: "h$= UŠ"
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: tommycoolman
Date: June 29, 2004 at 04:13:49 Pacific
Subject: Writing to file in C |
Reply: (edit)A couple things... 1. Instead of *buffer = *str, you should just use the strcpy() function. 2. When you fwrite(), you should write only the number of characters in the buffer. The following code worked for me.. -------------------- #include <stdio.h> int main () { FILE *pFile; char str[10]; char buffer[10]; gets(str); strcpy(buffer,str); pFile = fopen ("myfile.txt" , "w"); fwrite (buffer,1,strlen(buffer),pFile); fclose (pFile); return 0; } -------------------- Additionally, I would use scanf() instead of gets().
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
Name: the_merv
Date: June 29, 2004 at 06:55:35 Pacific
Subject: Writing to file in C |
Reply: (edit)Lets say i want to read that string from the file i cant use "fwrite (buffer,1,strlen(buffer),pFile);" becuase buffer has no length yet.
Report Offensive Follow Up For Removal
|
|
Response Number 5
|
Name: tommycoolman
Date: June 29, 2004 at 07:36:04 Pacific
Subject: Writing to file in C |
Reply: (edit)If you're using fread(), you don't need to tell fread() the length of the string, you only need to tell it the maximum length that the buffer can hold. ---------------- char buffer[256]; fread(buffer,1,255,pFile); ----------------
Report Offensive Follow Up For Removal
|
|
Response Number 6
|
Name: the_merv
Date: June 29, 2004 at 08:29:23 Pacific
Subject: Writing to file in C |
Reply: (edit)but when i read the string from the file depending on the length of the string it adds a "@" to the end of it and if the string is short then random characters.
Report Offensive Follow Up For Removal
|
|
Response Number 7
|
Name: tommycoolman
Date: June 29, 2004 at 16:34:37 Pacific
Subject: Writing to file in C |
Reply: (edit)Try using the bzero() function to fill the buffers with zero's before using them. char buffer[256]; bzero(buffer,256); fread(buffer,1,255,pFile);
Report Offensive Follow Up For Removal
|
Use following form to reply to current message:
|
|

|