array is full of the last element in a file - c

I have a procedure readTokens() which takes a file and an array and tokenizes the file and puts the things into the array but when I access the array, it is full of the last element. why? here is my readTokens() method:
void
readTokens(char *fileName, char** a[])
{
FILE *fp;
char *token;
int count = 0;
fp = fopen(fileName, "r");
if (fp == 0)
{
fprintf(stderr,"file %s could not be opened for reading\n", fileName);
exit(1);
}
token = readLine(fp);
while(!feof(fp))
{
a[count] = token;
++count;
free(token);
token = readLine(fp);
}
fclose(fp);
}

You should not call free(token), since you still have a pointer to it in a[count]. Since you're freeing it, that memory can be reused, and apparently it's being used when readLine() reads the next line. So each time you read a line, it reuses the same memory and overwrites it with the next line. As a result, all elements of a[] contain pointers to the same line.
a[count] = token doesn't make a copy of the token, it simply assigns the pointer.
This all assumes that readLine() uses malloc() to allocate the memory for the line it reads. If it doesn't, then it's even worse, since you must not call free() on memory that wasn't allocated with malloc() or calloc().
It's possible that readLine() is just using a static array variable, which would also explain why all your lines are the same. If you don't want it to keep overwriting the token, you need to make a copy of it in readTokens(). It would help if you posted the definition of readLine().

Related

fscanf not saving line - C

I'm trying to scan a line from a open file pointer, the one passed to the function is stdin. When I print out the value of input I get (null). Any ideas why fscanf is not saving the value? This is the code — also shown below:
char *ReadLineFile(FILE *infile){
char *input;
char buf[MAX];
//check if file pointer is at end of file
if(feof(infile))
return NULL;
//scan from file
fscanf(infile,"%s",input);
printf("%s",input);
input = (char *)malloc(strlen(input)+1);
//handle memory allocation errors
if (input == NULL){
printf("Error allocating memory\n");
return "error";
}
fclose(infile);
return input;
}
You must allocate memory for input before using fscanf(infile,"%s",input);.
fscanf(infile,"%s",input); asks fscanf to read a string and write it to input. Since input has not been assigned a value, let alone a value that points to memory allocated for this, the behavior is not defined.

Unable to print a char* string

It's not clear to me what's wrong with my program, it's a simple code to open a file, read the first line from it, and then print it. But the program keeps crashing. The actual content of my text file is a sentence: Test my code.
int main(void)
{
FILE *stream;
char *s;
stream = fopen("input.txt", "r");
fscanf(stream, " %s", &s);
printf("%s", s);
fclose(stream);
return 0;
}
I'm instructed not to use the library functions found in <string.h>
s is an uninitialized pointer. You need to allocate some memory for fscanf to write into.
char *s;
Allocates the number of bytes needed for holding a memory address (on most systems 32/64 bits).
But since you are not initializing the pointer, its value (the address it points to) is undefined.
Ergo: fscanf tries to write to an undefined memory address.
I initialized it as char *s = NULL;
Yes, the pointer is now initialized (yay!) but now points to nowhere.
Ergo: fscanf will try to write to nothing.
The solution is to allocate some memory fscanf can use.
fscanf does not magically allocate memory for you!
You can use either stack memory or dynamic allocated memory (heap).
Stack memory is easier to manage but is much smaller than the heap.
Here a solution which uses memory on the stack:
// Allocates 10 bytes on the stack
// Given 1 Character = 1 Byte the
// buffer can hold up to 9 characters.
char myBuffer[10];
// Initialize s with the address of myBuffer
char *s = myBuffer;
// Call fscanf
fscanf(stream, "%9s", s);
You may be wondering why I used %9s instead of %s.
The reason is to prevent a buffer overflow:
fscanf does not know how big the buffer is, so you need to tell it.
Otherwise fscanf will write data beyond your allocated memory.
I suggest you read up on C strings and memory management in general.
There are few things you are missing in your code.
// Uninitialise pointer, you need to allocate memory dynamically with malloc before you use it. That is
char *s;
int size = 20; // size of the string you want
s = malloc(size * sizeof(char));
// Or you can use a VLA and with this you don't have to use free(s)
char s[20];
// fopen could fail, always check the return value before using it.
stream = fopen("input.txt", "r");
if(stream == NULL){
perror("File opening failed");
return EXIT_FAILURE;
}
fscanf(stream, "%s", s);
//Don't forget to do free(s) to free memory allocated with malloc . when you are done with it

Segmentation Fault on fputs

I am pretty new to C and memory allocation in general. Basically what I am trying to do is copy the contents of an input file of unknown size and reverse it's contents using recursion. I feel that I am very close, but I keep getting a segmentation fault when I try to put in the contents of what I presume to be the reversed contents of the file (I presume because I think I am doing it right....)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int recursive_back(char **lines, int lineNumber, FILE *input) {
char *input_line = malloc(sizeof(char) * 1000);
lines = realloc(lines, (lineNumber) * 1000 * sizeof(char));
if(fgets(input_line, 201, input) == NULL) {
*(lines + lineNumber) = input_line;
return 1;
}
else {
printf("%d\n", lineNumber);
return (1+recursive_back(lines, ++lineNumber, input));
}
}
void backward (FILE *input, FILE *output, int debugflag ) {
int i;
char **lines; //store lines in here
lines = malloc(1000 * sizeof(char *) ); //1000 lines
if(lines == NULL) { //if malloc failed
fprintf(stderr, "malloc of lines failed\n");
exit(1);
}
int finalLineCount, lineCount;
finalLineCount = recursive_back(lines, 0, input);
printf("test %d\n", finalLineCount);
for(i = finalLineCount; i > 0; i--) {
fputs(*(lines+i), output); //segfault here
}
}
I am using a simple input file to test the code. My input file is 6 lines long that says "This is a test input file". The actual input files are being opened in another function and passed over to the backward function. I have verified that the other functions in my program work since I have been playing around with different options. These two functions are the only functions that I am having trouble with. What am I doing wrong?
Your problem is here:
lines = realloc(lines, (lineNumber) * 1000 * sizeof(char));
exactly as #ooga said. There are at least three separate things wrong with it:
You are reallocating the memory block pointed to by recursive_back()'s local variable lines, and storing the new address (supposing that the reallocation succeeds) back into that local variable. The new location is not necessarily the same as the old, but the only pointer to it is a local variable that goes out of scope at the end of recursive_back(). The caller's corresponding variable is not changed (including when the caller is recursive_back() itself), and therefore can no longer be relied upon to be a valid pointer after recursive_back() returns.
You allocate space using the wrong type. lines has type char **, so the object it points to has type char *, but you are reserving space based on the size of char instead.
You are not reserving enough space, at least on the first call, when lineNumber is zero. On that call, when the space requested is exactly zero bytes, the effect of the realloc() is to free the memory pointed to by lines. On subsequent calls, the space allocated is always one line's worth less than you think you are allocating.
It looks like the realloc() is altogether unnecessary if you can rely on the input to have at most 1000 lines, so you should consider just removing it. If you genuinely do need to be able to reallocate in a way that the caller will see, then the caller needs to pass a pointer to its variable, so that recursive_back() can modify it via that pointer.

Beginner C : Dynamic memory allocation

Switching to C from Java, and I'm having some troubles grasping memory management
Say I have a function *check_malloc that behaves as such:
// Checks if malloc() succeeds.
void *check_malloc(size_t amount){
void *tpt;
/* Allocates a memory block in amount bytes. */
tpt = malloc( amount );
/* Checks if it was successful. */
if ( tpt == NULL ){
fprintf(stderr, "No memory of %lu bytes\n", amount);
exit(1);
}
return tpt;
}
I also have the following variables to work with:
FILE *f = fopen("abc.txt", "r"); // Pointer to a file with "mynameisbob" on the first line and
// "123456789" on the second line
char *pname; // Pointer to a string for storing the name
}
My goal is to use *check_malloc to dynamically allocate memory so that the String pointed to by *pname is just the correct size for storing "mynamisbob", which is the only thing on the first line of the text file.
Here is my (failed) attempt:
int main(int argc, char *argv[]){
FILE *f = fopen("abc.txt", "r"); // A file with "mynameisbob" on the first line and
// "123456789" on the second line
char *pname; // Pointer to a string for storing the name
char currentline[150]; // Char array for storing current line of file
while(!feof(f)){
fgets(currentline,100,f);
pname = &currentline;
}
But I know this probably isn't the way to go about this, because I need to use my nice check_malloc* function.
Additionally, in my actual text file there is a "<" symbol before the name on the first line.But I just want the *pname to point to a String saying "mynameisbob" without the "<" symbol. This isn't that important now, it just is reinforcement to me that I know I can't just set the pointer to point straight to currentline.
Can anyone help me fix my thinking on this one? Thanks a lot.
In C you need to copy chars, not the "strings" (which are just pointers). Check out strcpy() and strlen(). Use strlen() to determine how long the line actually is which fgets has read, then use your malloc() to allocate exactly that (plus 1 for the 0). Then copy the chars over with strcpy().
There are several problems in your code, see my comments in this example:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Checks if malloc() succeeds.
void *check_malloc (size_t amount) {
void *tpt;
/* Allocates a memory block in amount bytes. */
tpt = malloc( amount );
/* Checks if it was successful. */
if (tpt == NULL) {
fprintf (stderr, "No memory of %lu bytes\n", amount);
exit (EXIT_FAILURE);
}
return tpt;
}
// To avoid subtle errors I have defined buffer size here
#define BUFFER_SIZE 150
// I have used the (void) version of main () here, while not strictly neccessary, you where not using argc and argv anyway, so the can be left out in this case
int main (void) {
// It might be a good idea to make the filename a char[] as well, but I leave that as an exercise to the reader.
FILE *f = fopen("abc.txt", "r"); // A file with "mynameisbob" on the first line and
// "123456789" on the second line
// You have to check whether the file was *actually openend*
if (f == NULL) {
fprintf (stderr, "Could not open file abc.txt\n"); // '"...%s\n", filename);' might better.
exit (EXIT_FAILURE);
}
char *pname; // Pointer to a string for storing the name
char currentline[BUFFER_SIZE]; // Char array for storing current line of file
while(!feof (f)) {
char *res = fgets (currentline, BUFFER_SIZE, f);
// fgets returns NULL when EOF was encountered before the next '\n'
if (res) {
size_t read = strlen (res);
// The line might have been empty
if (read) {
// Better use "sizeof *varname", while char is always 1 byte it is a good practice
pname = check_malloc ((read + 1) * sizeof *pname); // + 1 because we have to provide an extra char für '\0'
strncpy (pname, currentline, read); // You have to use strcpy or strncpy to copy the contents of the string rather than just assigning the pointer
// What was allocated must be freed again
free (pname);
}
}
}
fclose(f); // Always close everything you open!
return EXIT_SUCCESS;
}
Actually you really don't have to use pname in this simple case, because currentline already contains the line, but since you're trying to learn about memory management this should give you a general idea of how things work.
In your code you had this line:
pname = &currentline;
There are two problems here:
As already mentioned in my code assigning currentline to pname only copies the pointer not the contents.
The correct assignment would be pname = currentline (without the address operator &), because currentline is also a pointer under the hood (it behaves like char *currentline even though it's statically allocated).

How do I read lines from a file in C?

I need to read in a file. The first line of the file is the number of lines in the file and it returns an array of strings, with the last element being a NULL indicating the end of the array.
char **read_file(char *fname)
{
char **dict;
printf("Reading %s\n", fname);
FILE *d = fopen(fname, "r");
if (! d) return NULL;
// Get the number of lines in the file
//the first line in the file is the number of lines, so I have to get 0th element
char *size;
fscanf(d, "%s[^\n]", size);
int filesize = atoi(size);
// Allocate memory for the array of character pointers
dict = NULL; // Change this
// Read in the rest of the file, allocting memory for each string
// as we go.
// NULL termination. Last entry in the array should be NULL.
printf("Done\n");
return dict;
}
I put some comments because I know that's what I'm to do, but I can't seem to figure out how to put it in actual code.
To solve this problem you need to do one of two things.
Read the file as characters then convert to integers.
Read the file directly as integers.
For the first, you would use freed into a char array and then use atoi to convert to integer.
For the second, you would use fscanf and use the %d specify to read directly into an int variable;
fscanf does not allocate memory for you. Passing it a random pointer as you have will only cause trouble. (I recommend avoid fscanf).
The question code has a flaw:
char *size;
fscanf(d, "%s[^\n]", size);
Although the above may compile, it will not function as expected at runtime. The problem is that fscanf() needs the memory address of where to write the parsed value. While size is a pointer that can store a memory address, it is uninitialized, and points to no specific memory in the process' memory map.
The following may be a better replacement:
fscanf(d, " %d%*c", &filesize);
See my version of the spoiler code here

Resources