943,571 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1427
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 18th, 2009
0

Reading Problem in C

Expand Post »
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. }
Similar Threads
Reputation Points: 8
Solved Threads: 3
Junior Poster
bharanidharanit is offline Offline
140 posts
since Nov 2008
Jun 18th, 2009
0

Reversing in C

Hello,
I need string Reversing in C
Read the output of a file as

ABCDEFGHIFSFSA
1242487132
FSFSA
*
Print the output as

*
FSFSA
1242487132
ABCDEFGHIFSFSA
Reputation Points: 8
Solved Threads: 3
Junior Poster
bharanidharanit is offline Offline
140 posts
since Nov 2008
Jun 18th, 2009
0

Re: Reversing in C

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!
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jun 18th, 2009
0

Re: Reading Problem in C

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!
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jun 18th, 2009
0

Re: Reversing in C

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?
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Jun 18th, 2009
0

Re: Reversing in C

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
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jun 18th, 2009
0

Re: Reversing in C

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".
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jun 18th, 2009
0

Re: Reversing in C

Quote ...
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.

Quote ...
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.

Quote ...
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.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Jun 18th, 2009
-1

Re: Reversing in C

Quote ...
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.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Jun 18th, 2009
0

Re: Reversing in C

Click to Expand / Collapse  Quote originally posted by Tom Gunn ...
. "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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,947 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Large numbers
Next Thread in C Forum Timeline: Adding datas of two files in C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC