I am reading a string from a file. After like the second or third time the function gets executed, one or more random characters become appended to the buffer string and I have no idea why that happens.
Here's the piece of code:
scorefile = fopen("highscore.dat", "rb");
if (scorefile)
{
fseek(scorefile, 0, SEEK_END);
length = ftell(scorefile);
fseek(scorefile, 0, SEEK_SET);
buffer = malloc(length);
if (buffer)
{
fread(buffer, 1, length, scorefile);
}
fclose(scorefile);
}
Am I doing something wrong here?
Let's spell it all out and go slightly more robust:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
char *loadScoreFile(const char *filename)
{
char *buffer = NULL;
FILE *scorefile = fopen(filename, "r");
if (scorefile != NULL)
{
(void) fseek(scorefile, 0, SEEK_END);
int length = ftell(scorefile);
(void) fseek(scorefile, 0, SEEK_SET);
buffer = malloc(length + 1);
if (buffer != NULL)
{
assert(length == fread(buffer, 1, length, scorefile));
buffer[length] = '\0';
}
(void) fclose(scorefile);
}
return buffer;
}
int main()
{
for (int i = 0; i < 10; i++)
{
char *pointer = loadScoreFile("highscore.dat");
if (pointer != NULL)
{
printf("%s", pointer);
free(pointer);
}
}
return 0;
}
if you use buffer = malloc(length);, and then read length bytes into it, it will be one byte too short. Char arrays in C are zero-terminated, so they need an extra byte to but that zero. buffer = malloc(length+1); will fix this.
Related
I am writing a program in C. I use low level functions like open, read, close. I have a file descriptor, etc, but I don't know how to print only the first 2 lines from a file that has e.g. 30 lines of text. how to do it?
you need to read a file into a string, iterate through the string, concat any character into the string variable, define a int variable for lines count, when lines reaches 2, break the loop
here’s an example how you can do it
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *get_first_two_lines(char *file_name) {
FILE *file = fopen(file_name, "r");
fseek(file, 0, SEEK_END);
long size = ftell(file);
fseek(file, 0, SEEK_SET);
char *buffer = malloc(size);
fread(buffer, 1, size, file);
char *two_lines = calloc(1, sizeof(char));
unsigned int lines = 0;
for (int i=0;i<strlen(buffer);i++) {
if (lines == 2) break;
if (buffer[i] == '\n') {
if (lines < 1) {
two_lines = realloc(two_lines, (strlen(two_lines) + 2) * sizeof(char));
strcat(two_lines, (char []) {'\n', 0});
}
lines++;
continue;
}
two_lines = realloc(two_lines, (strlen(two_lines) + 2) * sizeof(char));
strcat(two_lines, (char []) {buffer[i], 0});
}
return two_lines;
}
int main(int argc, char *argv[]) {
char *first_two_lines = get_first_two_lines("file_name");
printf("%s", first_two_lines);
return 0;
}
char* freadline(FILE* fp){
fseek(fp, 0, SEEK_END);
int lSize = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *buffer = malloc(lSize);
fread(buffer, 1, lSize, fp);
fgets(buffer, sizeof(lSize), fp);
return buffer;
}
but it doesn't read line by line any suggestions as to how this would be read line by line
There are couple solutions here.
The first is to get the size of the entire file using fseek and ftell fseek will allow you to go to the end of file and ftell will give you the current position which can be used as a size indicator. You can then allocate enough of a buffer to read the entire file then split them up into lines.
The other solution is to use a temporary buffer of 1000 or so like you're already doing, read a character at a time using fgetc in a loop and feed it into the temporary buffer until you hit a new line indicator , then use the strlen method to get the length and allocate a buffer of that size, copy the temporary buffer then return the allocated buffer.
There is also errors in your code as pointed out in the comments. You're discarding your allocated memory resulting in a leak. And your freadline doesn't actually read a line it just reads whatever size you're telling it to read.
the lines in the file could be of any length.
realloc() is a classic approach, but how about a simple, slow and plodding one:
Read once to find line length, seek, allocate, then read again to save the line.
#include <stdio.h>
char* freadline(FILE *fp) {
int length = 0;
long offset = ftell(fp);
if (offset == -1)
return NULL;
int scan_count = fscanf(fp, "%*[^\n]%n", &length); // Save scan length
if (scan_count == EOF)
return NULL;
if (fseek(fp, offset, SEEK_SET))
return NULL;
size_t n = length + 1u; // +1 for potential \n
char *buf = malloc(n + 1); // + 1 for \0
if (buf == NULL)
return NULL;
size_t len = fread(buf, 1, n, fp);
buf[len] = '\0';
return buf;
}
Test
#include <assert.h>
#include <stdlib.h>
int main() {
FILE *fp = fopen("tmp.txt", "w+");
assert(fp);
for (int i = 0; i < 10; i++) {
int l = i * 7;
for (int j = 0; j < l; j++) {
fputc(rand() % 26 + 'a', fp);
}
fputc('\n', fp);
}
rewind(fp);
char *s;
while ((s = freadline(fp)) != NULL) {
printf("<%s>", s);
free(s);
}
fclose(fp);
return 0;
}
Output
<
><lvqdyoq
><ykfdbxnqdquhyd
><jaeebzqmtblcabwgmscrn
><oiaftlfpcuqffaxozqegxmwgglkh
><vxtdhnzqankyprbwteazdafeqxtijjtkwea
><zqgmplohyxrutojvbzllqgjaidbtqibygdzcxkujvw
><ghwbmjjmbpksnzkgzgiluiggpkzwhaetclrcyxcsixsutjmrm
><vqlybsjnihnfqyfhyszwgpsvnhnngdnjzjypqcflnztrhcfgbkakzxam
><alsuauxxchqjxqaiddtjszgcbullyyjymytioyawpzshhfpqpsatddbcagjgobm
>
If you're ok targeting POSIX, it already has a function that does what you need: getline.
#include <stdio.h>
#include <stdlib.h>
FILE *fh = ...;
char *line = NULL;
size_t buf_size = 0;
while (1) {
ssize_t line_len = getline(&line, &buf_size, fh);
if (line_len == -1)
break;
// ...
}
free(line);
If not, getline can be implemented using using fgets and realloc in a loop. Just start with a arbitrarily-sized buffer.
I have code for copying a text file (not binary) into an array of chars.
I am trying to copy the contents of a .txt file into a char* array.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
bool is_not_binary(const void *data, size_t len)
{
return memchr(data, '\0', len) != NULL;
}
int main(void)
{
char* file_name="./bash_example.sh";
FILE *file = fopen (file_name, "r");
size_t size = 64;
const size_t line_size = 300;
char *mem = malloc(size);
if (mem == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
*mem = 0;
if (file != NULL && is_not_binary(file_name,line_size)) {
char* line = malloc(line_size);
while (fgets(line, line_size, file) != NULL) {
size_t total = strlen(mem) + strlen(line) + 1;
if (size < total) {
size_t newsize = (total & ~1U) << 1;
char *tmp = realloc(mem, newsize);
if (tmp == NULL) {
perror("realloc");
exit(EXIT_FAILURE);
}
mem = tmp;
size = newsize;
}
strcat(mem, line);
}
}
printf("%s",mem);
return 0;
}
But in my code I must specify a static size:
size_t size = 64;
const size_t line_size = 300;
I want to remove this and I want dynamic allocation, is it possible?
Actually it's static code, 64 and 300.
I coded up an example that makes use of fopen, fread, and the classic example doubling the buffer size as the content grows.
int main()
{
const size_t initial_size = 1024;
char* contents = (char*)malloc(initial_size);
size_t length = 0;
size_t allocated = initial_size;
FILE* file = fopen("./sbatch_example.sh", "r");
if (file)
{
while (1)
{
size_t remaining = allocated - length;
size_t result = 0;
if (remaining == 0)
{
contents = (char*)realloc(contents, allocated*2);
allocated = allocated*2;
remaining = allocated-length;
}
result = fread(contents+length, 1, remaining, file);
length += result;
if (result==0) /* EOF */
{
break;
}
}
}
if (file)
{
fclose(file);
file = NULL;
}
/* at this point, "contents" is your file data bytes
and "length" is the number of bytes copied into that array*/
/*optional: append a null char to the end of the buffer to make it easier for debugging and print statements */
contents = (char*)realloc(contents, length+1);
contents[length] = '\0';
free(contents);
return 0;
}
You can use ftell() to get the total length in advance and fread() to read a whole at one time as below.
FILE *fp = fopen("./sbatch_example.sh", "r");
if (fp) {
fseek(fp, 0, SEEK_END);
size_t size = ftell(fp);
rewind(fp);
char *mem = malloc(size+1);
size_t nr = fread(mem, size, 1, fp);
}
Here, I am trying to read from a text file, copy this file into an array, then I want to write the array to another text file. This is not copying into the array at all. I am just getting blank values when I print.
int main(void)
{
char char_array[50];
char copied_array[50];
//int n = 2;
FILE* fpointer = fopen("hello_world.txt", "r");
FILE* fpointer2 = fopen("copyhello.txt", "w");
for(int i = 0;i < 50; i++)
{
fread(&char_array, sizeof(char), 1, fpointer);
copied_array[i] = char_array[i];
}
for(int j = 0;j < 50; j++)
{
printf("char_array: %c\n", copied_array[j]);
}
fclose(fpointer);
fclose(fpointer2);
}
working code. hope this becomes clearer :)
Note you're using fread/fwrite - compare with fgets/fputs for strings.
#include "stdio.h"
#include "string.h"
#define BUFSIZE 50
// memory size 'plus one' to leave room for a string-terminating '\0'
#define BUFMEMSIZE (BUFSIZE+1)
const char *file1 = "hello_world.txt";
const char *file2 = "copyhello.txt";
int main(void)
{
char char_array[BUFMEMSIZE];
char copied_array[BUFMEMSIZE];
FILE *fInput, *fOutput;
fInput = fopen(file1, "r");
if(fInput != NULL)
{
fOutput = fopen(file2, "w");
if(fOutput != NULL)
{
// make sure memory is wiped before use
memset(char_array, 0, BUFMEMSIZE);
memset(copied_array, 0, BUFMEMSIZE);
size_t lastSuccessfulRead = 0;
// the read-then-loop pattern: try and read 50 chars
size_t bytesRead = fread(char_array, sizeof(char), BUFSIZE, fInput);
while(bytesRead != 0)
{
// we got at least 1 char ..
// (to be used at end - so we know where in char_array is the last byte read)
lastSuccessfulRead = bytesRead;
// 'bytesRead' bytes were read : copy to other array
strncpy(copied_array, char_array, bytesRead);
// write to output file, number of bytes read
fwrite(copied_array, sizeof(char), bytesRead, fOutput);
// read more, and loop, see if we got any more chars
bytesRead = fread(char_array, sizeof(char), BUFSIZE, fInput);
}
// set char after the last-read-in char to null, as a string-terminator.
char_array[lastSuccessfulRead] = '\0';
// an array of chars is also a 'string'
printf("char_array: %s\n", char_array);
fclose(fOutput);
}
else printf("cant open %s\n", file2);
fclose(fInput);
}
else printf("cant open %s\n", file1);
}
When I write my string to file, I first write the length of the string as an int, followed by the string itself. Here is my code:
int wordLength = strlen(words);
fwrite(&wordLength,sizeof(int),1, outputFile);
fwrite(&words,sizeof(char),strlen(words), outputFile);
However, when I fread it back, I get an empty string. Here is my reading code:
int strLength;
fread(&strLength, sizeof(int), 1, f);
char* word = (char*) malloc(strLength*sizeof(char));
fread(&word, sizeof(char), strLength, f);
Why is this happening?
when I fread it back, I get an empty string. Here is my reading code:
Why is this happening?
fread(&strLength, sizeof(int), 1, f);
char* word = (char*) malloc(strLength*sizeof(char));
fread(&word, sizeof(char), strLength, f);
Code allocates insufficient memory. strLength*sizeof(char) is enough for the text yet not the terminating null character to make a string.
// char* word = (char*) malloc(strLength*sizeof(char));
char* word = malloc(strLength + 1u); // add 1
fread(&word, ...); is attempting to read data into the address of word, rather than into the memory just allocated.
// fread(&word, sizeof(char), strLength, f);
fread(word, sizeof *word, strLength, f); // drop &
The null character is never appended.
size_t count = fread(word, sizeof *word, strLength, f);
if (count != strLength) puts("Error");
else {
word[strLength] = '\0';
puts(word);
}
Notes:
Better to use size_t wordLength
Checking the return value of malloc() makes for good code.
size_t wordLength = strlen(words);
...
char* word = malloc(strLength + 1);
if (word == NULL) Hanlde_OutOfMemory();
Post does not show file open/closing details. Code may need to rewind(f) before reading data written.
This works on Ubuntu:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv)
{
FILE *outputFile;
FILE *inputFile;
char words[] = "This is a series of words";
int wordLength = strlen(words);
outputFile = fopen("outputFile", "w");
if ( outputFile == NULL )
{
perror("fopen failed: ");
exit(1);
}
fwrite(&wordLength,sizeof(int),1, outputFile);
fwrite(words,sizeof(char),strlen(words), outputFile);
fclose(outputFile);
inputFile = fopen("outputFile", "r");
if ( inputFile == NULL )
{
perror("fopen(2) failed: ");
exit(1);
}
int strLength = -99;
fread(&strLength, sizeof(int), 1, inputFile);
char* buff = (char*) malloc(strLength*sizeof(char));
fread(buff, sizeof(char), strLength, inputFile);
buff[strLength] = 0x00;
printf("Input Str: -->%s<--\n", buff);
}