| | |
C file stream
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
•
•
Quick Question: Is there a way in c to read an entire file into a string or char buffer?
Thanks, Elise
[EDIT]
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> int main() { char * buff = NULL; int fLength = 0; FILE * file = NULL; if ((file = fopen("doc.txt", "r")) == NULL) { printf("No file \n"); return 1; } fseek(file, 0, SEEK_END); fLength = ftell(file); if ((buff = malloc(fLength * sizeof(char) + 1)) == NULL) { fclose(file); return 1; } fseek(file, SEEK_SET, 0); fread(buff, sizeof(char), fLength, file); buff[fLength + 1] = '\0'; puts(buff); free(buff); fclose(file); return 0; }
Last edited by andor; Feb 9th, 2007 at 5:16 am.
If you want to win, you must not loose (Alan Ford)
Except simply seeking to the end doesn't take into account any translations which may occur when the file is read byte by byte (even if you do read them all in one call).
http://c-faq.com/osdep/filesize.html
http://c-faq.com/osdep/filesize.html
![]() |
Similar Threads
- Making a log file global (C++)
- Need Help in Reading characters from a text file (C++)
- Getting all data from an input and output file (C++)
- ofstream - detect if file has been deleted between open and close (C++)
- A question about file streaming (C++)
- inputing system("date /t") and system("time /t") into a file (C++)
- file pointer increament (C)
- Saving a file using C++ (C++)
Other Threads in the C Forum
- Previous Thread: My own read function
- Next Thread: not sure what this error means
Views: 5595 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C
#include * .net append array arrays asterisks binarysearch calculate changingto char character cm command copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fgets file fork forloop framework function functions givemetehcodez grade graphics gtkwinlinux hacking histogram homework include incrementoperators input intmain() iso kernel keyboard km lazy license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft mqqueue mysql number oddnumber odf opensource overwrite owf pdf performance pointer pointers posix probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket spoonfeeding standard string student systemcall testing threads turboc unix user variable wab whythiscodecausesegmentationfault windowsapi






