Write To Files Help!
Author |
Message |
Nikola

|
Posted: Sun Jun 22, 2008 1:06 pm Post subject: Write To Files Help! |
|
|
I dont understand what I am doing wrong! I continue to get these annoying errors no matter what I do! Ill post the code below and hope that one of you see my mistake
code: | #include <stdio.h>
#include <stdlib.h>
int main( )
{
FILE *fil_ptr;
char words[40];
int int_ctr = 0;
fil_ptr = fopen("C:\Users\Nikola\Desktop\code.dat", "w");
printf("Please enter a name: ");
scanf(" %s", words);
fprintf(fil_ptr, "\n%s", words);
fclose(fil_ptr);
system("PAUSE");
return 0;
}
|
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
btiffin

|
Posted: Sun Jun 22, 2008 1:33 pm Post subject: RE:Write To Files Help! |
|
|
A few things to check into.
C escapes backslashes in string literals, so be careful with the filename. Try C:\\ ... \\ ...
Check fil_ptr for NULL, if the open for write does not work (privs, disk space, ...) fil_ptr will test equal to 0.
Another hint (a little more advanced but keeps bad guys out) use %40s in your scanf. It will stop potential crashes/buffer overflow attacks when someone types 41 or more characters.
Keep up the good works
Cheers |
|
|
|
|
 |
Nikola

|
Posted: Sun Jun 22, 2008 1:46 pm Post subject: RE:Write To Files Help! |
|
|
Alright I fixed it by adding the extra slash Thanks again btiffin your amazing  |
|
|
|
|
 |
md

|
Posted: Sun Jun 22, 2008 3:58 pm Post subject: RE:Write To Files Help! |
|
|
you'd actually want "%39s" because of the \0 terminating character  |
|
|
|
|
 |
btiffin

|
Posted: Sun Jun 22, 2008 4:38 pm Post subject: RE:Write To Files Help! |
|
|
Good catch md.
Off by one; yet another woodpecker taking out civilization.
Cheers |
|
|
|
|
 |
btiffin

|
Posted: Sun Jun 22, 2008 4:49 pm Post subject: Re: Write To Files Help! |
|
|
Oh and in case that last joke went over anyone's head. It's from this famous quote by Gerald Weinberg Quote: ?If builders built buildings the way programmers wrote programs, the first woodpecker to come along would destroy civilization?
And it really is true; the first time I tried submitting the above, I got
code: |
Could not insert new word matches
DEBUG MODE
SQL Error : 1271 Illegal mix of collations for operation ' IN '
INSERT INTO v3_search_wordmatch (post_id, word_id, title_match) SELECT 166279, word_id, 0 FROM v3_search_wordlist WHERE word_text IN ('case', 'last', 'joke', 'anyones', 'head', 'famous', 'gerald', 'weinberg', '?if', 'builders', 'built', 'buildings', 'programmers', 'programs', 'first', 'woodpecker', 'destroy', 'civilization?')
Line : 226
File : functions_search.php
|
Cheers |
|
|
|
|
 |
Nikola

|
Posted: Sun Jun 22, 2008 7:09 pm Post subject: RE:Write To Files Help! |
|
|
Thanks for the help guys but i have another problem. Now i am trying to read a dat file. Basicly i want it to ask a person to type a letter and when they do it reads a file. I tried fscanf and fgets but i cant figure it out! Can someone help help me out im quite confused! |
|
|
|
|
 |
btiffin

|
Posted: Sun Jun 22, 2008 9:04 pm Post subject: Re: Write To Files Help! |
|
|
Yeah;
fgets will only read up to the first newline. newlines being stored on disk as ascii 13, ascii 10, or both. CR, LF or CRLF, but C sometimes cares, sometimes doesn't. fgets will read a line from the file. Sucking in an entire file at a time is a different issue.
Use REBOL for that, data: read %data.dat Sorry, that was me advertising my favourite.
For a look at fread() and some nice code that fetches the size, allocates the right ram and then slurps in the whole file at once check out http://www.cplusplus.com/reference/clibrary/cstdio/fread.html I know it says cplusplus, don't worry, this code is C.
Cheers |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Nikola

|
Posted: Sun Jun 22, 2008 11:05 pm Post subject: RE:Write To Files Help! |
|
|
Still not too sure how to work it. I just want it too print the contents of whats in the dat file. |
|
|
|
|
 |
btiffin

|
Posted: Sun Jun 22, 2008 11:57 pm Post subject: Re: Write To Files Help! |
|
|
At this stage I'd like to know if this is self-learning or an assignment.
I don't want to deny you a chance to learn any course material. The struggle is what makes the grade.
Cheers |
|
|
|
|
 |
Nikola

|
Posted: Mon Jun 23, 2008 8:49 am Post subject: RE:Write To Files Help! |
|
|
Its for a summative. Im done everything except reading the file and and making it go back to the menu which is quite annoying because i've tried my best to get it too work and yet no solution has come up Also sorry for not posting right away my isp was acting strange due to a power outage but i believe they are back now. |
|
|
|
|
 |
btiffin

|
Posted: Mon Jun 23, 2008 12:28 pm Post subject: RE:Write To Files Help! |
|
|
Never worry about time lapse between posts. We all (are supposed to) have lives.
Moving to the Read to help thread. |
|
|
|
|
 |
|
|