Reading Problem in C

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2008
Posts: 117
Reputation: bharanidharanit is an unknown quantity at this point 
Solved Threads: 2
bharanidharanit bharanidharanit is offline Offline
Junior Poster

Reading Problem in C

 
0
  #1
Jun 18th, 2009
Hello,
I want to read a text file in C and the output file must be created and want to reverse the data in tat output file created.
So i used the below coding but not reversing the data in the output
file created.
eg: input file contains this data
abc
123
output file must be
123
abc
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define MAX_STRING_LENGTH 1000
  6. #define BUFFER_SIZE 50
  7.  
  8. /* Global variables */
  9. FILE *pInFile = NULL; /* File pointer to input file */
  10. FILE *pOutFile = NULL; /* File pointer to output file */
  11. char *infilename = "C:\\myfile.txt"; /* Name of the file to be read */
  12. char *outfilename = "C:\\outfile.txt"; /* Name of the file to be written */
  13. char *buffer = NULL;
  14. size_t buffer_size = BUFFER_SIZE;
  15.  
  16.  
  17. void main()
  18. {
  19. size_t str_length = 0;
  20. int str_count = 0;
  21. fpos_t *positions = NULL;
  22. int i = 0;
  23.  
  24. buffer = (char*)malloc(buffer_size); /* Create initial buffer */
  25.  
  26. if((pInFile = fopen(infilename, "r")) == NULL) /* Open the input file */
  27. {
  28. printf("Error opening %s for reading. Program terminated.", infilename);
  29. abort();
  30. }
  31.  
  32. /* Find out how many strings there are */
  33. for(;;)
  34. {
  35. fread(&str_length, sizeof(size_t), 1, pInFile); /* Read the string length */
  36. if(feof(pInFile)) /* If it is end of file */
  37. break; /* We are finished */
  38.  
  39. /* Check buffer is large enough and increase if necessary */
  40. if(str_length>buffer_size)
  41. {
  42. buffer_size = str_length+1;
  43. free(buffer);
  44. buffer = (char*)malloc(buffer_size);
  45. }
  46. fread(buffer, str_length, 1, pInFile); /* Read the string */
  47. ++str_count;
  48. }
  49. printf("\nThere are %d strings in the input file.", str_count);
  50.  
  51. /* Now get the position for the beginning of each record in the file */
  52. /* The buffer is now large enough to hold the longest string */
  53. rewind(pInFile);
  54. positions = (fpos_t*)malloc(str_count*sizeof(fpos_t)); /* Array to store the positions */
  55. for(i = 0 ; i<str_count ; i++)
  56. {
  57. fgetpos(pInFile, positions+i); /* Get the positions */
  58. fread(&str_length, sizeof(size_t), 1, pInFile); /* Read the string length */
  59. fread(buffer, str_length, 1, pInFile); /* Read the string */
  60. }
  61.  
  62. /* Open the output file */
  63. if((pOutFile = fopen(outfilename, "w")) == NULL)
  64. {
  65. printf("Error opening %s for reading. Program terminated.", outfilename);
  66. abort();
  67. }
  68.  
  69. /* Read the records in reverse order from the input file and write to the new file */
  70. for(i = 0 ; i<str_count ; i++)
  71. {
  72. fsetpos(pInFile, positions+str_count-i-1); /* Set the file position */
  73. fread(&str_length, sizeof(size_t), 1, pInFile); /* Read the string length */
  74. fwrite(&str_length, sizeof(size_t), 1, pOutFile); /* Write to new file */
  75. fread(buffer, str_length, 1, pInFile); /* Read the string */
  76. fwrite(buffer, str_length, 1, pOutFile); /* Write to new file */
  77. }
  78.  
  79. fclose(pInFile); /* Close input file */
  80. fclose(pOutFile); /* Close output file */
  81. printf("\nNew file write complete\n");
  82.  
  83. /* List contents of output file */
  84. if((pOutFile = fopen(outfilename, "r")) == NULL) /* Open the new file to read it */
  85. {
  86. printf("Error opening %s for reading. Program terminated.", outfilename);
  87. abort();
  88. }
  89. printf("\nThe strings in the new file are:");
  90. for(i = 0 ; i<str_count ; i++)
  91. {
  92. fread(&str_length, sizeof(size_t), 1, pOutFile);
  93. fread(buffer, str_length, 1, pOutFile);
  94. buffer[str_length] = '\0';
  95. printf("\n%s", buffer);
  96. }
  97. printf("\n");
  98. fclose(pOutFile); /* Close file */
  99.  
  100. /* Free the memory we allocated */
  101. if(buffer != NULL)
  102. free(buffer);
  103. if(positions != NULL)
  104. free(positions);
  105. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 117
Reputation: bharanidharanit is an unknown quantity at this point 
Solved Threads: 2
bharanidharanit bharanidharanit is offline Offline
Junior Poster

Reversing in C

 
0
  #2
Jun 18th, 2009
Hello,
I need string Reversing in C
Read the output of a file as

ABCDEFGHIFSFSA
1242487132
FSFSA
*
Print the output as

*
FSFSA
1242487132
ABCDEFGHIFSFSA
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Reversing in C

 
0
  #3
Jun 18th, 2009
hint: One function and use multiple times for double reverse.

Reverse entire string.
Parse whitespace, mark 1st and last character per word, then reverse that data, skip whitespace repeat!

have fun!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Reading Problem in C

 
0
  #4
Jun 18th, 2009
I see you're planning to give the drive some exercise!

May I suggest an alternative? Read the entire file iinto memory, then parse it in memory by working from end of load to beginning of load. Saves all that hard drive thrashing!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 133
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: Reversing in C

 
0
  #5
Jun 18th, 2009
Use recursion:
  1. #include <stdio.h>
  2.  
  3. void PrintFileBackward(FILE *fp);
  4.  
  5. int main()
  6. {
  7. FILE *fp = fopen("test.dat", "r");
  8. PrintFileBackward(fp);
  9. return 0;
  10. }
  11.  
  12. void PrintFileBackward(FILE *fp)
  13. {
  14. char s[20];
  15. if (!fgets(s, 20, fp)) return;
  16. PrintFileBackward(fp);
  17. fputs(s, stdout);
  18. }
Each time you call PrintFileBackward, the next line is read from the file. Then when the functions are rolling back up the stack, each line is printed. But because lines are printed from the inside out in the opposite order that they were read, they get printed in reverse.

Cool, huh?
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Reversing in C

 
0
  #6
Jun 18th, 2009
With that solution increase your buffer size slightly (Not too much as its recursion) and added an fopen() error check and don't forget your fclose().
Also note "rt" on the fopen() if you need your line terminator translation.
/r/n vs /n
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Reversing in C

 
0
  #7
Jun 18th, 2009
Tom Gunn, please read these:
http://www.daniweb.com/forums/thread78060.html
http://www.daniweb.com/forums/announcement118-2.html
There's a line between "help" and "spoon-feeding the complete answer".
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 133
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: Reversing in C

 
0
  #8
Jun 18th, 2009
With that solution increase your buffer size slightly
It should be no less than the largest expected line length. I picked 20 because none of the lines in the example file were longer than that.

don't forget your fclose()
You don't need to fclose() if the program ends. When the program ends normally, all open files are flushed and closed.

Also note "rt" on the fopen() if you need your line terminator translation.
"r" is already text mode where whatever line terminator combination is used on the OS, it becomes '\n' inside your program. "rb" is binary mode, and those translations aren't made. "rt" isn't portable.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 133
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: Reversing in C

 
-1
  #9
Jun 18th, 2009
There's a line between "help" and "spoon-feeding the complete answer".
If I cross the line then one of the moderators will let me know, but I'll keep those links in mind when I post.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,575
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1485
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Reversing in C

 
0
  #10
Jun 18th, 2009
Originally Posted by Tom Gunn View Post
. "rt" isn't portable.
Whatever gave you that idea? True, text mode is the default, but there is nothing wrong with specifying 't' in the mode string. If there was then it would be in the c standards.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC