| | |
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
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks bash binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory dynamic fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






