Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

C file input

Name: Anonymous 2011-10-30 8:27

hey /prog/, i have a C problem

i am making a hang man game
everytime a session is launched, the player choses to play against a player (who inputs a word) or the computer (which selects words from a file)

the thing is, i want to write the words the player inputs inside of the string
if the string is already in the file, it's not added, if not it is added

so far i am able to write things in a file, but every time i start a new session, the new inputted text overwrite what was there before

i am using fopen("derp.txt", "r+");

Name: Anonymous 2011-10-30 8:28

the player inputs inside of the text*

Name: Anonymous 2011-10-30 8:48

i have a C problem
Now you've got an infinite amount of 2EXPTIME-complete problems.

Name: FrozenVoid 2011-10-30 8:51

Use append mode "ab"

Name: FrozenVoid 2011-10-30 8:52

http://www.cplusplus.com/reference/clibrary/cstdio/fopen/
"a+"    Open a file for reading and appending. All writing operations are performed at the end of the file, protecting the previous content to be overwritten. You can reposition (fseek, rewind) the internal pointer to anywhere in the file for reading, but writing operations will move it back to the end of file. The file is created if it does not exist.

Name: Anonymous 2011-10-30 9:03

ok instead of r+ its a

Name: Anonymous 2011-10-30 9:05

Thanks Frozen Void.

Name: Anonymous 2011-10-30 13:26

>>4
That doesn't work if the OP is running this on Linux you stupid fucker. Do you know the correct way? I bet not. Again you're stupid. And again, I think you just need to kill yourself.

Name: Anonymous 2011-10-30 13:33

>>5
And the code sucks. I'm gonna make reference to C89 standard as I break this down for your stupid ass. Try and follow along. Maybe your stupid ass will learn something along the way...

/* fopen example */
#include <stdio.h>
int main ()

main() isn't 100% correct. However, most compilers will let it pass.

{
  FILE * pFile;
  pFile = fopen ("myfile.txt","w");
  if (pFile!=NULL)
  {
    fputs ("fopen example",pFile);

fputs might block. If that happens, you won't see any kind of output to the standard output.

    fclose (pFile);

This may not actually close the file. On top of that, this could produce a memory leak.

  }
  return 0;

This might not actually terminate the program on some platforms.
}

Don't change these.
Name: Email:
Entire Thread Thread List