How JustAnswer Works:

  • Ask an Expert
    Experts are full of valuable knowledge and are ready to help with any question. Credentials confirmed by a Fortune 500 verification firm.
  • Get a Professional Answer
    Via email, text message, or notification as you wait on our site.
    Ask follow up questions if you need to.
  • 100% Satisfaction Guarantee
    Rate the answer you receive.

Ask Ingo U Your Own Question

Ingo U
Ingo U, Software Engineer
Category: Programming
Satisfied Customers: 755
Experience:  Over 25 years experience in software development. Expert in Microsoft .Net, C#, C++, VB, SQL
44076068
Type Your Programming Question Here...
Ingo U is online now

Program to print the contents of a file in reverse order using

Resolved Question:

program to print the contents of a file in reverse order using dynamically allocated memory as temporary storage
Find out how big the file is
Use malloc to allocate enough memory on the heap
Treat the memory you have allocated like an array of characters
Print the array (allocated memory) in reverse order
Submitted: 1 year ago.
Category: Programming
Customer: replied 1 year ago.
(Posted by JustAnswer at customer's request) Hello. I would like to request the following Expert Service(s) from you: Live Phone Call. Let me know if you need more information, or send me the service offer(s) so we can proceed.
Expert:  Ingo U replied 1 year ago.
Straight C code I presume?
Customer: replied 1 year ago.
YES IT IS
Expert:  Ingo U replied 1 year ago.
What OS/compiler are you on?
Customer: replied 1 year ago.
code blocks
Expert:  Ingo U replied 1 year ago.
linux?
Customer: replied 1 year ago.
no i can't talk on the phone as i don't have enough money to pay this out
Expert:  Ingo U replied 1 year ago.
I don't want to talk on the phone, just want to know if you're on windows or linux.
Customer: replied 1 year ago.
Ok i am on windows
Expert:  Ingo U replied 1 year ago.
Try this:https://www.dropbox.com/s/tkrna6jpamemu9k/file-rev.c?dl=0https://www.dropbox.com/s/1hl706rpxgfh2h7/data.txt?dl=0The program assumes the file "data.txt" is present in the working directory where the program runs. Please let me know if you need anything else
Customer: replied 1 year ago.
i want to read the file from keyboard
Expert:  Ingo U replied 1 year ago.
You mean the name of the file?
Customer: replied 1 year ago.
yes
Expert:  Ingo U replied 1 year ago.
I've updated the program:https://www.dropbox.com/s/tkrna6jpamemu9k/file-rev.c?dl=0
Customer: replied 1 year ago.
like thisprintf("\n Please Input a filename: ");
gets(name);
infile= fopen(name,"r");if (infile == NULL) {
printf(" Error opening file \n");
exit(1);
}
Customer: replied 1 year ago.
sorry ignore the last one
Customer: replied 1 year ago.
hi again can you tell me what is this code does
stat(filepath, &statbuf);
Customer: replied 1 year ago.
sorry again to be honest with i did nt understand anything as i am student and this my 3rd assignment but i have to follow what i already learning however i knew that you answered the question but is there any chance to make simple program that does the job as followingFind out how big the file is (there are multiple ways to do this, one way is to count the number of characters in the file.)
Use malloc to allocate enough memory on the heap
Treat the memory you have allocated like an array of characters, re-read the file and store each character in the allocated memory
Print the array (allocated memory) in reverse order
Expert:  Ingo U replied 1 year ago.
OK, well stat() is a standard C library function to obtain information about a file, including the size...without opening it and reading/counting every character. But we can do it that way, if you prefer. Stand by...
Expert:  Ingo U replied 1 year ago.
Here you go:https://www.dropbox.com/s/tkrna6jpamemu9k/file-rev.c?dl=0Better?
Expert:  Ingo U replied 1 year ago.
Hi,
just following up, were you able to use my solution?
Please feel free to post any additional questions, comments or concerns you may have regarding this issue.
If you're not yet satisfied with the result, please let me know what is missing.
I will do my best to make it right for you.
Otherwise, please don't forget to rate my service - I don't get paid for my work until your rating is received.
Regards,
Ingo U
Customer: replied 1 year ago.
Hi Thank you very much for your help
everything working fine but i have some question ?"1" first what this function exactly does ?
fread(databuf, 1, size, in file);"2" why did you open and close the file after each code like following ?infile= fopen(name,"r");
databuf = (char *)malloc(size);
fread(databuf, 1, size, infile);
fclose(infile);"3" the code can work without
#include
#include
#include
so why did you in cloud them ?again thank you
Customer: replied 1 year ago.
include
include
nclude
Customer: replied 1 year ago.
fcntl.h
sys/types.h
sys/stat.h
Expert:  Ingo U replied 1 year ago.
Here's documentation on fread:http://www.tutorialspoint.com/c_standard_library/c_function_fread.htmYou have to open a file before you can get data from it, and you should close it when you're done, although your code will work even if you don't.The #includes for those files were my attempts to find the right one, I couldn't remember which one made system functions like open/fread etc available. I just forgot to remove the unsuccessful ones. Having them there does not affect the functionality of the code.
Expert:  Ingo U replied 1 year ago.
Hi,just following up, were you able to use my solution? Please feel free to post any additional questions, comments or concerns you may have regarding this issue.If you're not yet satisfied with the result, please let me know what is missing. I will do my best to make it right for you.Otherwise, please don't forget to rate my service - I don't get paid for my work until your rating is received.Regards,Ingo U
Ingo U, Software Engineer
Category: Programming
Satisfied Customers: 755
Experience: Over 25 years experience in software development. Expert in Microsoft .Net, C#, C++, VB, SQL
Ingo U and other Programming Specialists are ready to help you
Customer: replied 1 year ago.
Hi IngoYes everything is totally fine thank you very much for your help i have giving you 5 star feedback
i have another question need to be answered
can i post here ?

What Customers are Saying:

 
 
 
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer
< Previous | Next >
  • My Expert answered my question promptly and he resolved the issue totally. This is a great service. I am so glad I found it I will definitely use the service again if needed. One Happy Customer
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C.
  • This expert is wonderful. They truly know what they are talking about, and they actually care about you. They really helped put my nerves at ease. Thank you so much!!!! Alex
  • Thank you for all your help. It is nice to know that this service is here for people like myself, who need answers fast and are not sure who to consult. GP
  • I couldn't be more satisfied! This is the site I will always come to when I need a second opinion. Justin
  • Just let me say that this encounter has been entirely professional and most helpful. I liked that I could ask additional questions and get answered in a very short turn around. Esther
  • Wonderful service, prompt, efficient, and accurate. Couldn't have asked for more. I cannot thank you enough for your help. Mary C.
 
 
 

Meet The Experts:

 
 
 
  • ATLPROG

    ATLPROG

    Computer Software Engineer

    Satisfied Customers:

    7260
    MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
< Previous | Next >
  • http://ww2.justanswer.com/uploads/SP/spatlanta2010/2011-6-23_12450_photo.64x64.gif ATLPROG's Avatar

    ATLPROG

    Computer Software Engineer

    Satisfied Customers:

    7260
    MS in IT.Several years of programming experience in Java C++ C C# Python VB Javascript HTML
  • http://ww2.justanswer.com/uploads/ComputersGuru/2010-02-13_051118_Photo41.JPG LogicPro's Avatar

    LogicPro

    Computer Software Engineer

    Satisfied Customers:

    5514
    Expert in C, C++, Java, DOT NET, Python, HTML, Javascript, Design.
  • http://ww2.justanswer.com/uploads/lifesaver333/2010-10-17_191349_ls.jpeg lifesaver's Avatar

    lifesaver

    Computer Software Engineer

    Satisfied Customers:

    936
    Several years of intensive programming and application development experience in various platforms.
  • http://ww2.justanswer.com/uploads/EH/ehabtutor/2012-8-2_202016_1.64x64.jpg ehabtutor's Avatar

    ehabtutor

    Computer Software Engineer

    Satisfied Customers:

    766
    Bachelor of computer science, 5+ years experience in software development, software company owner
  • http://ww2.justanswer.com/uploads/eljonis/2010-01-06_130406_eljon2.jpg Eljon's Avatar

    Eljon

    Consultant

    Satisfied Customers:

    558
    Founder of StockCanvas.com
  • http://ww2.justanswer.com/uploads/FA/fael/2011-1-15_104041_profile500x500.64x64.jpg Rafael Martins's Avatar

    Rafael Martins

    Computer Software Engineer

    Satisfied Customers:

    446
    Desktop, Mobile and Web Developer. 7+ years of experience. Creative solutions provider.
  • http://ww2.justanswer.com/uploads/CH/chutz747/2011-11-6_14231_me.64x64.jpg The-PC-Guy's Avatar

    The-PC-Guy

    Software Engineer

    Satisfied Customers:

    320
    Extensive Knowledge in PHP, MYSQL, CSS & Javascript