Open file append byte example code

Hi, I am a C# developer, and I am starting to get interested in C. I am currently rewriting a C# app to C and I have been looking for some example code for a while now, and I have been unable to find anything that will help me. I really could use some example code that does the following, and I don't think it would take to long to write.

Open file.
Read last byte.
if byte is maxed (doesnt a single byte max at 255?)
-> Create new byte, append to file, save.
else
-> byte++, rewrite last byte, save.
print file to console

... I learn best by example, and a complete program that does this would teach me a lot, thank you.

Open in new window

LVL 1
addicktzAsked:
Who is Participating?
 
Jaime OlivaresConnect With a Mentor Software ArchitectCommented:
you nee something like:


void main()
{
    FILE *fp;
    unsigned char byte;
 
    fp = fopen("somefile.dat", "w+b");        /* open file in read/write binary mode */
    fseek(fp, -1, SEEK_END);  /* go to the end of the file */
    fread(&byte, 1, 1, fp);  /* read last byte */ 
 
    if (c==255)
    {
           fseek(fp, 0, SEEK_END);  /* just to append */
           byte = 0;   /* some value */
           fwrite(&byte, 1, 1, fp);   /* append byte */
    }
    else
   {
           fseek(fp, -1, SEEK_END);  /* just to overwrite last byte */
           byte++;   /* increment */
           fwrite(&byte, 1, 1, fp);   /* overwrite byte */
    }
}

Open in new window

0
 
Jaime OlivaresSoftware ArchitectCommented:
forgot  fclose(fp) at the end of main(), also you will need this at the beginning of the code:

#include <studio.h>
0
 
addicktzAuthor Commented:
thats absolutely perfect, thank you so much, how can I use fprintf to display the contents of that file?
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Jaime OlivaresSoftware ArchitectCommented:
it is a binary file, how do you like to display this binary information?
0
 
addicktzAuthor Commented:
as text (ascii)
0
 
Jaime OlivaresSoftware ArchitectCommented:
if you try to show as text directly you will have a lot of garbage on the screen.
0
 
addicktzAuthor Commented:
yes, I know this is an odd request, the fprintf of this example is actually for a different piece of code, i am just using this code to rewrite my code, im thinking its something like

???? fileContents = fread(???
fprintf(????, fileContents);
0
 
Jaime OlivaresSoftware ArchitectCommented:
no, printf() won't work with binary streams, because a ascii-0 character will stop it.
you can use fwrite() to write to a file.
notice fprintf() will print to a file, printf() will print to the console, if you want to print to the console anyway you can use fwrite(some_buffer, some_size, 1, stdout).
stdout is a special handler that represents the console.
0
 
addicktzAuthor Commented:
ok awesome, thanks a lot, how would i read the contents of a file into a variable?
0
 
Jaime OlivaresSoftware ArchitectCommented:
something like this:

    fread(&byte, 1, 1, fp);  /* read last byte */

1st argument: pointer to variable
2nd argument: size of variable (1 byte in this case)
3rd argument: element count (1, unless you are reading an array of elements)
4th argument: file handler
0
 
addicktzAuthor Commented:
so it would be something like

char contents;
fp = fopen("sometext.txt", "r");
fread(&contents,sizeOf(fp),sizeOf(fp),fp);
fprintf(stdout, contents);

?
0
 
Jaime OlivaresSoftware ArchitectCommented:
sizeof(fp) won't give you the file size, if that is you are trying to do
if you specify char as the container variable, you should use sizeof(contents), which gives you 1
using sizeof() in the third argument won't work again

if you want to read all the entire contents of the file and then print, then you will need to:
open the file,
go to end of file with fseek()
use ftell() to know the file size
go to init of file with fseek()
dinamically allocate a memory portion with malloc() and get a pointer
read the file contents into the allocated memory()
close the file()
print all the contents using fwrite(), not fprintf()
deallocate the memory with free()

but this is a matter of other question...
0
 
addicktzAuthor Commented:
ok awesome, thanks, im getting an error with this malloc....

        char contents;
      fp = fopen("somefile.dat", "r");
      fseek(fp, -1, SEEK_END);
      int somel = ftell(fp);
      fseek(fp, -1, SEEK_SET);  
      contents = malloc(somel);      
      fread(&contents,somel,somel,fp);
      fwrite(&contents,somel,somel,stdout);
      fclose(fp);

does this look right?
0
 
Jaime OlivaresSoftware ArchitectCommented:
close, some mods:

  char contents;
      fp = fopen("somefile.dat", "rb");
      fseek(fp, -1, SEEK_END);
      int somel = ftell(fp);
      fseek(fp, 0, SEEK_SET);  
      contents = malloc(somel);      
      fread(&contents,somel,1,fp);    // dont read somel*somel bytes !!!
      fwrite(&contents,somel,1,stdout);
      fclose(fp);
      free(contents);  // free the allocation to avoid a leak
0
 
addicktzAuthor Commented:
amazing thank you =]
0
 
addicktzAuthor Commented:
hrmmm, i dont think byte==255 is working
0
 
Jaime OlivaresSoftware ArchitectCommented:
in my code says:
   if (c==255)
 it should be:
   if (byte==255)
 
it should work, if not, try to test with printf("byte read=%i\r\n", (int)byte);
0
 
addicktzAuthor Commented:
ya its saying that byte == 8 everytime
0
 
addicktzAuthor Commented:


#include <stdio.h>
 
int main()
{
    FILE *fp;
    unsigned char byte;
 
    fp = fopen("somefile.dat", "w+b");        /* open file in read/write binary mode */
    fseek(fp, -1, SEEK_END);  /* go to the end of the file */
    fread(&byte, 1, 1, fp);  /* read last byte */
 
    printf("byte read=%i\r\n", (int)byte);
 
    if (byte==2)
    {
           fseek(fp, 0, SEEK_END);  /* just to append */
           byte = 0;   /* some value */
           fwrite(&byte, 1, 1, fp);   /* append byte */
    }
    else
    {
           fseek(fp, -1, SEEK_END);  /* just to overwrite last byte */
           byte++;   /* increment */
           fwrite(&byte, 1, 1, fp);   /* overwrite byte */
    }
 
    fflush(0);
 
    fclose(fp);
 
    return 0;
}

Open in new window

0
 
Jaime OlivaresSoftware ArchitectCommented:
have you checked the real file contents? is it 08 the last byte in file?
0
 
addicktzAuthor Commented:
i havent, but I started the file over, and let the program create it, also, if 08 is the last byte in the file it should increment, but it keeps saying 8 everytime i run it, i just read on

http://visualcplus.blogspot.com/2006/02/lesson-6-ascii-code-and-character.html

that this should be working, if the fact is that it is an 8, is there something wrong to where its not over writing the last byte?
0
 
addicktzAuthor Commented:
also, I just changed the

byte == 2

to

byte == 8

and it didnt grow to two bytes...
0
 
addicktzAuthor Commented:
ok, i just added a printf after the byte increment, and it went to 9, so the original byte number is 8....why is the file not saving?
0
 
Jaime OlivaresSoftware ArchitectCommented:
I think it is related with the file mode, change this line:
      fp = fopen("somefile.dat", "w+b");
to:
      fp = fopen("somefile.dat", "r+b");
 
0
 
addicktzAuthor Commented:
will do, thanks again for your help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.