Another solution would be to map the entire file to the memory and then treat it as a char array.
Up vote 1 down vote favorite share g+ share fb share tw.
I am reaing from a file, and when I read, it takes it line by line, and prints it what I want exactly is I want an array of char holding all the chars in the file and print it once, this is the code I have if(strcmp(str0,"@")==0) { FILE *filecomand; //char fname40; char line100; int lcount; ///* Read in the filename */ //printf("Enter the name of a ascii file: "); //fgets(History. Txt, sizeof(fname), stdin); /* Open the file. If NULL is returned there was an error */ if((filecomand = fopen(str1, "r")) == NULL) { printf("Error Opening File.
\n"); //exit(1); } lcount=0; int i=0; while( fgets(line, sizeof(line), filecomand)! = NULL ) { /* Get each line from the infile */ //lcount++; /* print the line number and data */ //printf("%s", line); } fclose(filecomand); /* Close the file */ c file arrays char read link|improve this question asked Jun 4 '10 at 16:18Nadeem Tabbaa62.
3 Duplicate of reading a text file into an array in c – James McNellis Jun 4 '10 at 16:20 actually what I want is the whole text file content is held in one char array, not for printing,, I want to use the char of array later – Nadeem Tabbaa Jun 4 '10 at 16:21 2 Nadeem, see the accepted answer that he linked. It's what you want. Basically, that bytes char* is the array you're talking about, and you can do whatever you want with it until you free it.
– SB. Jun 4 '10 at 16:47.
Another solution would be to map the entire file to the memory and then treat it as a char array. Under windows MapViewOfFile, and under unix mmap. Once you mapped the file (plenty of examples), you get a pointer to the file's beginning in the memory.
Cast it to char.
You need to determine the size of the file. Once you have that, you can allocate an array large enough and read it in a single go. There are two ways to determine the size of the file.
Using fstat: struct stat stbuffer; if (fstat(fileno(filecommand), &stbuffer)! = -1) { // file size is in stbuffer. St_size; } With fseek and ftell: if (fseek(fp, 0, SEEK_END)!
= 0) { off_t size = ftell(fp) if (size! = -1) { // succesfully got size } // Go back to start of file fseek(fp, 0, SEEK_SET); }.
Since you can't assume how big the file is, you need to determine the size and then dynamically allocate a buffer. I won't post the code, but here's the general scheme. Use fseek() to navigate to the end of file, ftell() to get size of the file, and fseek() again to move the start of the file.
Allocate a char buffer with malloc() using the size you found. The use fread() to read the file into the buffer. When you are done with the buffer, free() it.
Use a different open. I.e. Fd = open(str1, O_RDONLY|O_BINARY) /* O_BINARY for MS */ The read statement would be for a buffer of bytes.
Count = read(fd,buf, bytecount) This will do a binary open and read on the file.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.