H
Henk
Hi Guys,
(see actual code below)
I wrote a little program that is supposed to read a file (input.txt)
into memory (using a stack) and then reverse the file and display it to
output. It works, but I still get some errors when i try to compile
it:
stack.c: In function 'reverseFile'
stack.c:98: warning:int format, pointer arg (arg 2)
And when I run the program It does reverse the input.txt file and
displays it to output, but I am getting 2 more errors:
free() invalid pointer 0x804a178!
and in the end i get a:
Segmentation fault.
I know it is not the best C code ever, I am just getting started.I did
some printf's in the code just for testing where it gets.
I hope you can help me.
Greetings Henk
The code--------------------
-----------stack.c-------------------
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
struct buffer* readFileToBuffer(char *filename){
struct buffer *first;
struct buffer *current;
FILE *file =fopen(filename,"r");
if(file==NULL){
printf("Unable to open: %s\n", filename);
exit(1);
}
first = createBuffer();
current = first;
while( !(feof(file))){ //VERANDERD
//leest hele file in, maakt buffers aan en zet de file daarin
current->size=
fread(current->data,(sizeof(char)),BUFFER_SIZE,file);
if(current->size < BUFFER_SIZE){
if(ferror(file) != 0){
printf("file error, readFileToBuffer()\n");
clearerr(file);
exit(1);
}
if(feof(file) !=0){
printf("end of file\n");
}
}
if(feof(file)==0){
//alleen maken als feof nog niet gezet is. Anders overbodige
buffer.
struct buffer *nextbuf=createBuffer();
current->next = nextbuf;
current=current->next;
}
}
clearerr(file);
fclose(file);
return first;
}
struct buffer* createBuffer(){
struct buffer *buf;
buf=malloc(sizeof(struct buffer));
if (buf==NULL){
printf("Not possible to allocate memory\n");
exit(1);
}
buf->size = 0;
buf->next = NULL;
buf->data = malloc(sizeof(char)*BUFFER_SIZE);
if (buf->data == NULL){
printf("Not possible to allocate memory\n");
exit(1);
}
return buf;
}
void printNormalFile (struct buffer *firstElem){
struct buffer *current = firstElem;
while((*current).next != NULL){
printf((*current).data);
printf("current->lenght=%d\n",current->size);
current = (*current).next;
}
printf((*current).data);
printf("current->lenght=%d\n",current->size);
}
void reverseFile(struct buffer *first){
printf("frst->lenght=%d\n",first->size);
struct buffer *current = first;
struct buffer *previousElm = NULL;
int laatsteElm;
int i;
char *kopie;
while(1){
if(current==NULL){
break;
}
while(current->next != NULL){ //laatste buffer zoeken
previousElm=current;
current=current->next;
printf("\na\n");
}
printf("\ncurrent->lng=%d\n",current->size);
printf("prev=%d\n",previousElm);
laatsteElm = current->size;
kopie=(current->data)+(current->size)-1;
for(i=0;i < laatsteElm;i++){
printf("%c",*kopie);
kopie--;
}
free(current->data);
free(current);
current=first;
if(previousElm==NULL){
return;
}
previousElm->next=NULL;
}
}
int main(int argc, char** argv){
char *filename=argv[1];
struct buffer *first=readFileToBuffer(filename);
printf("firstsize=%d",first->size);
printNormalFile(first);
reverseFile(first);
return 0;
}
-------------------------------------------------------------------
---------stack.h---------------------------
#ifndef STACK
#define STACK
#define BUFFER_SIZE 128
#include <stdlib.h>
struct buffer{
char *data;
struct buffer *next;
int size;
};
struct buffer* readFileToBuffer(char *filename);
struct buffer* createBuffer();
void printNormalFile (struct buffer *firstElem);
void reverseFile(struct buffer *first);
#endif
-------------------------------------------------
(see actual code below)
I wrote a little program that is supposed to read a file (input.txt)
into memory (using a stack) and then reverse the file and display it to
output. It works, but I still get some errors when i try to compile
it:
stack.c: In function 'reverseFile'
stack.c:98: warning:int format, pointer arg (arg 2)
And when I run the program It does reverse the input.txt file and
displays it to output, but I am getting 2 more errors:
free() invalid pointer 0x804a178!
and in the end i get a:
Segmentation fault.
I know it is not the best C code ever, I am just getting started.I did
some printf's in the code just for testing where it gets.
I hope you can help me.
Greetings Henk
The code--------------------
-----------stack.c-------------------
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
struct buffer* readFileToBuffer(char *filename){
struct buffer *first;
struct buffer *current;
FILE *file =fopen(filename,"r");
if(file==NULL){
printf("Unable to open: %s\n", filename);
exit(1);
}
first = createBuffer();
current = first;
while( !(feof(file))){ //VERANDERD
//leest hele file in, maakt buffers aan en zet de file daarin
current->size=
fread(current->data,(sizeof(char)),BUFFER_SIZE,file);
if(current->size < BUFFER_SIZE){
if(ferror(file) != 0){
printf("file error, readFileToBuffer()\n");
clearerr(file);
exit(1);
}
if(feof(file) !=0){
printf("end of file\n");
}
}
if(feof(file)==0){
//alleen maken als feof nog niet gezet is. Anders overbodige
buffer.
struct buffer *nextbuf=createBuffer();
current->next = nextbuf;
current=current->next;
}
}
clearerr(file);
fclose(file);
return first;
}
struct buffer* createBuffer(){
struct buffer *buf;
buf=malloc(sizeof(struct buffer));
if (buf==NULL){
printf("Not possible to allocate memory\n");
exit(1);
}
buf->size = 0;
buf->next = NULL;
buf->data = malloc(sizeof(char)*BUFFER_SIZE);
if (buf->data == NULL){
printf("Not possible to allocate memory\n");
exit(1);
}
return buf;
}
void printNormalFile (struct buffer *firstElem){
struct buffer *current = firstElem;
while((*current).next != NULL){
printf((*current).data);
printf("current->lenght=%d\n",current->size);
current = (*current).next;
}
printf((*current).data);
printf("current->lenght=%d\n",current->size);
}
void reverseFile(struct buffer *first){
printf("frst->lenght=%d\n",first->size);
struct buffer *current = first;
struct buffer *previousElm = NULL;
int laatsteElm;
int i;
char *kopie;
while(1){
if(current==NULL){
break;
}
while(current->next != NULL){ //laatste buffer zoeken
previousElm=current;
current=current->next;
printf("\na\n");
}
printf("\ncurrent->lng=%d\n",current->size);
printf("prev=%d\n",previousElm);
laatsteElm = current->size;
kopie=(current->data)+(current->size)-1;
for(i=0;i < laatsteElm;i++){
printf("%c",*kopie);
kopie--;
}
free(current->data);
free(current);
current=first;
if(previousElm==NULL){
return;
}
previousElm->next=NULL;
}
}
int main(int argc, char** argv){
char *filename=argv[1];
struct buffer *first=readFileToBuffer(filename);
printf("firstsize=%d",first->size);
printNormalFile(first);
reverseFile(first);
return 0;
}
-------------------------------------------------------------------
---------stack.h---------------------------
#ifndef STACK
#define STACK
#define BUFFER_SIZE 128
#include <stdlib.h>
struct buffer{
char *data;
struct buffer *next;
int size;
};
struct buffer* readFileToBuffer(char *filename);
struct buffer* createBuffer();
void printNormalFile (struct buffer *firstElem);
void reverseFile(struct buffer *first);
#endif
-------------------------------------------------