#Making a ToDo List but....

33 messages · Page 1 of 1 (latest)

worthy nimbus
#

I want the tasks to stay but instead the data in the csv file gets deleted as soon as as i end the program the csv data gets deleted

wary jackalBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

compact badge
#

as soon as I delete the code
what?

worthy nimbus
#

i mean

#

as soon as i end the program

compact badge
#

How are you writing to the file?
How do you know the data is there while the program is running?

worthy nimbus
#

the csv data gets deleted

#

wait

#

here's the code

compact badge
#

Or did you mean that the file was previously containing text, but after the program execution it completely erased all content or did it really remove the file?

worthy nimbus
#

no

#

it erased the content

#

so when the next time i ran the program

compact badge
#

That's probably because you opened the file in w mode

worthy nimbus
#

the tasks i added last time were deleted

#

ohh

#

so should i append them

compact badge
#

Yeah, this line here:

FILE *file = fopen("todolist.csv", "w");
```opens the `todolist.csv` file and truncates it (i.e. clears it)
worthy nimbus
#

instead

#

aaah i see

compact badge
#

!man fopen

wary jackalBOT
#

fopen, fdopen, freopen - stream open functions

Synopsis
#include <stdio.h>

FILE *fopen(const char *restrict pathname, const char *restrict mode);
FILE *fdopen(int fd, const char *mode);
FILE *freopen(const char *restrict pathname, const char *restrict mode,
              FILE *restrict stream);

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

fdopen():
    _POSIX_C_SOURCE

compact badge
#

w Truncate file to zero length or create text file for
writing. The stream is positioned at the beginning of the
file.

worthy nimbus
#

so should i open the file with a+ attribute?

#

@compact badge

compact badge
#

a or a+. Whatever fits your needs better I guess.

worthy nimbus
#

ohkay cool

#

thank you so much

compact badge
#

a if you only want to write to it, a+ if you also want to be able to read

worthy nimbus
#

i recently learnt file handling so been making a few mistakes

compact badge
#

a Open for appending (writing at end of file). The file is
created if it does not exist. The stream is positioned at
the end of the file.

   a+     Open for reading and appending (writing at end of file).
          The file is created if it does not exist.  Output is
          always appended to the end of the file.  POSIX is silent
          on what the initial read position is when using this mode.
          For glibc, the initial file position for reading is at the
          beginning of the file, but for Android/BSD/MacOS, the
          initial file position for reading is at the end of the
          file.