| | |
Reading Problem in C
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 120
Reputation:
Solved Threads: 2
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
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
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_STRING_LENGTH 1000 #define BUFFER_SIZE 50 /* Global variables */ FILE *pInFile = NULL; /* File pointer to input file */ FILE *pOutFile = NULL; /* File pointer to output file */ char *infilename = "C:\\myfile.txt"; /* Name of the file to be read */ char *outfilename = "C:\\outfile.txt"; /* Name of the file to be written */ char *buffer = NULL; size_t buffer_size = BUFFER_SIZE; void main() { size_t str_length = 0; int str_count = 0; fpos_t *positions = NULL; int i = 0; buffer = (char*)malloc(buffer_size); /* Create initial buffer */ if((pInFile = fopen(infilename, "r")) == NULL) /* Open the input file */ { printf("Error opening %s for reading. Program terminated.", infilename); abort(); } /* Find out how many strings there are */ for(;;) { fread(&str_length, sizeof(size_t), 1, pInFile); /* Read the string length */ if(feof(pInFile)) /* If it is end of file */ break; /* We are finished */ /* Check buffer is large enough and increase if necessary */ if(str_length>buffer_size) { buffer_size = str_length+1; free(buffer); buffer = (char*)malloc(buffer_size); } fread(buffer, str_length, 1, pInFile); /* Read the string */ ++str_count; } printf("\nThere are %d strings in the input file.", str_count); /* Now get the position for the beginning of each record in the file */ /* The buffer is now large enough to hold the longest string */ rewind(pInFile); positions = (fpos_t*)malloc(str_count*sizeof(fpos_t)); /* Array to store the positions */ for(i = 0 ; i<str_count ; i++) { fgetpos(pInFile, positions+i); /* Get the positions */ fread(&str_length, sizeof(size_t), 1, pInFile); /* Read the string length */ fread(buffer, str_length, 1, pInFile); /* Read the string */ } /* Open the output file */ if((pOutFile = fopen(outfilename, "w")) == NULL) { printf("Error opening %s for reading. Program terminated.", outfilename); abort(); } /* Read the records in reverse order from the input file and write to the new file */ for(i = 0 ; i<str_count ; i++) { fsetpos(pInFile, positions+str_count-i-1); /* Set the file position */ fread(&str_length, sizeof(size_t), 1, pInFile); /* Read the string length */ fwrite(&str_length, sizeof(size_t), 1, pOutFile); /* Write to new file */ fread(buffer, str_length, 1, pInFile); /* Read the string */ fwrite(buffer, str_length, 1, pOutFile); /* Write to new file */ } fclose(pInFile); /* Close input file */ fclose(pOutFile); /* Close output file */ printf("\nNew file write complete\n"); /* List contents of output file */ if((pOutFile = fopen(outfilename, "r")) == NULL) /* Open the new file to read it */ { printf("Error opening %s for reading. Program terminated.", outfilename); abort(); } printf("\nThe strings in the new file are:"); for(i = 0 ; i<str_count ; i++) { fread(&str_length, sizeof(size_t), 1, pOutFile); fread(buffer, str_length, 1, pOutFile); buffer[str_length] = '\0'; printf("\n%s", buffer); } printf("\n"); fclose(pOutFile); /* Close file */ /* Free the memory we allocated */ if(buffer != NULL) free(buffer); if(positions != NULL) free(positions); }
Use recursion:
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?
c Syntax (Toggle Plain Text)
#include <stdio.h> void PrintFileBackward(FILE *fp); int main() { FILE *fp = fopen("test.dat", "r"); PrintFileBackward(fp); return 0; } void PrintFileBackward(FILE *fp) { char s[20]; if (!fgets(s, 20, fp)) return; PrintFileBackward(fp); fputs(s, stdout); }
Cool, huh?
-Tommy (For Great Justice!) Gunn
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".
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".
•
•
•
•
With that solution increase your buffer size slightly
•
•
•
•
don't forget your fclose()
•
•
•
•
Also note "rt" on the fopen() if you need your line terminator translation.
-Tommy (For Great Justice!) Gunn
![]() |
Similar Threads
- txt reading problem (C++)
- query problem case syntax error can't figure it out (MS SQL)
- *this problem (help?) (C++)
- file handling problem (C++)
- formated file reading problem (C++)
- Reading and writing from files works selectively... (C++)
- No Signal Problem (Troubleshooting Dead Machines)
- Internet Connection problem:Cable to modem to router to PC (Networking Hardware Configuration)
- Really headache problem in win XP- pls help (Windows NT / 2000 / XP)
Other Threads in the C Forum
- Previous Thread: Large numbers
- Next Thread: Adding datas of two files in C
Views: 878 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi






