Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6959299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:15:13+00:00 2026-05-27T15:15:13+00:00

I wrote this code to test to combine two files: long getFileSize(char *filename) {

  • 0

I wrote this code to test to combine two files:

 long getFileSize(char *filename)
{
     FILE* fp=fopen(filename,"rb");
     fseek(fp,0,SEEK_END);
     long size=ftell(fp); 
     fclose(fp); 
     return size;   
}



 long lengthA = getFileSize(argv[1]);
   long lengthB = getFileSize(argv[2]);
   printf("sizeof %s is:%d\n",argv[1],lengthA);
   printf("sizeof %s is %d\n",argv[2],lengthB);

   void *pa;
   void *pb;
   FILE* fp=fopen(argv[1],"rb");
   fread(pa,1,lengthA,fp);
   fclose(fp);
   FILE* fpn=fopen(argv[2],"rb");
   fread(pb,1,lengthB,fpn);
   fclose(fpn);
   printf("pointerA is:%p;pointerB is:%p\n",pa,pb);

   FILE *ff=fopen("test.pack","wb");
   fwrite(pa,1,lengthA,ff);
   fwrite(pb,1,lengthB,ff);
   fclose(ff);

   long lengthFinal = getFileSize("test.pack");

   printf("Final size:%i\n",lengthFinal);

however I don’t know if the data is equal to the returned value from getFileSize,the console print clearly says something wrong with it,but I can’t figure it out:

sizeof a.zip is:465235
sizeof b.zip is 107814
pointerA is:0x80484ec;pointerB is:0x804aff4
Final size:255270

since I know the length of each file,I can then use fseek to restore them right? that’s the idea I was thinking.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:15:14+00:00Added an answer on May 27, 2026 at 3:15 pm

    *pa and *pb need to point to some memory where the file content shall be read to.

    So, do a malloc for these two buffers with lengthA*sizeof(char) and lengthB*sizeof(char) and pass these allocated buffers to fread:

    pa = malloc(lengthA*sizeof(char));
    pb = malloc(lengthB*sizeof(char));
    ...
    fread(pa,sizeof(char),lengthA,fp);
    ...
    fread(pb,sizeof(char),lengthB,fpn);
    

    Furthermore, fread returns the number of items actually read. Also check this!

    Excerpt from man fread:

    fread() and fwrite() return the number of items successfully read or written (i.e., not the number of characters). If an error occurs, or the end-of-file is reached, the return value is a short item count (or zero).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi guys I wrote this code and i have two errors. Invalid rank specifier:
I wrote this short test code, but it didn't work. What am I doing
I'm running some test to prove a concept and i just wrote this code
I wrote some test code like this which compiled and worked fine... void threadtest()
I wrote this simple test code, by adapting a piece from a book, to
I just wrote this test code in my CustomUIPanel class: public static void main(String[]
I wrote this code at the end of a long day: MyObject *thisObj; //
I wrote this small piece of code to test something: #include <stdio.h> int main()
I wrote this code I have these errors Cannot implicitly convert type x.Program.TreeNode' to
I wrote this code in C# to encrypt a string with a key: private

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.