?
Solved

Open file append byte example code

Posted on 2007-12-06
25
Medium Priority
?
1,204 Views
Last Modified: 2013-11-14
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

0
Comment
Question by:addicktz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 11
25 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 2000 total points
ID: 20424571
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20424575
forgot  fclose(fp) at the end of main(), also you will need this at the beginning of the code:

#include <studio.h>
0
 
LVL 1

Author Comment

by:addicktz
ID: 20424593
thats absolutely perfect, thank you so much, how can I use fprintf to display the contents of that file?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20424613
it is a binary file, how do you like to display this binary information?
0
 
LVL 1

Author Comment

by:addicktz
ID: 20424621
as text (ascii)
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20424631
if you try to show as text directly you will have a lot of garbage on the screen.
0
 
LVL 1

Author Comment

by:addicktz
ID: 20424647
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20424665
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
 
LVL 1

Author Comment

by:addicktz
ID: 20424673
ok awesome, thanks a lot, how would i read the contents of a file into a variable?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20424693
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
 
LVL 1

Author Comment

by:addicktz
ID: 20424709
so it would be something like

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

?
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20424739
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
 
LVL 1

Author Comment

by:addicktz
ID: 20424799
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20424810
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
 
LVL 1

Author Comment

by:addicktz
ID: 20424822
amazing thank you =]
0
 
LVL 1

Author Comment

by:addicktz
ID: 20424867
hrmmm, i dont think byte==255 is working
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20424884
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
 
LVL 1

Author Comment

by:addicktz
ID: 20424889
ya its saying that byte == 8 everytime
0
 
LVL 1

Author Comment

by:addicktz
ID: 20424925


#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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20424928
have you checked the real file contents? is it 08 the last byte in file?
0
 
LVL 1

Author Comment

by:addicktz
ID: 20424947
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
 
LVL 1

Author Comment

by:addicktz
ID: 20424954
also, I just changed the

byte == 2

to

byte == 8

and it didnt grow to two bytes...
0
 
LVL 1

Author Comment

by:addicktz
ID: 20424967
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20425001
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
 
LVL 1

Author Comment

by:addicktz
ID: 20425011
will do, thanks again for your help
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just about everyone has an old PC laying around.  Ask anyone in the IT industry, whether they are a professional or play in it as a hobby.  From outdated Desktops to cheap "throwaway" laptops, they are all around and not as hard to "fix up" as you m…
Windows 10 is here and for most admins this means frustration and challenges getting that first working Windows 10 image. As in my previous sysprep articles, I've put together a simple help guide to get you through this process. The aim is to achiev…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question