Hi, This program reads parameters from parameter line : program.exe data.txt result.txt (or somth.) , then takes date from data.txt and writes it to buffer, then program divides buffer to lines , writes to stack, and finally it should pop lines to file, but there is an error doing that...
any solutions ?
#include <stdio.h>
#include <stdlib.h>
#define MAXSTACK 10
#define EMPTYSTACK -1
//reading parameters from paramaters line f.e.: program data.txt answ.txt
char *fill_buffer(char *filename)
{
FILE *fp;
long filesize;
char *buffer;
if (!(fp = fopen(filename, "rb")))
{
printf("mistake: can't open file\n");
exit(EXIT_FAILURE);
}
fseek(fp, 0, SEEK_END);
filesize = ftell(fp);
rewind(fp);
if (!(buffer = (char*)calloc(filesize + 1, sizeof(char))))
{
printf("mistake: memory problems\n");
exit(EXIT_FAILURE);
}
if (filesize != fread(buffer, sizeof(char), filesize, fp))
{
printf("mistake: can't read file\n");
exit(EXIT_FAILURE);
}
buffer[filesize] = '\0';
fclose(fp);
return buffer;
};
//------------------------------------------------------------------------------
int main(int argc, char **argv)
{
int top = EMPTYSTACK;
char items[MAXSTACK];
void push(char *c) {
items[++top] = *c;
}
char pop() {
return items[top--];
}
int full() {
return top+1 == MAXSTACK;
}
int empty() {
return top == EMPTYSTACK;
}
void push( char*);
char pop();
int empty();
int full();
char *DELIMITERS = "\n";
char *str, *buffer, *temp;
char last_char;
FILE *fr;
if (argc != 3) {printf ("entered wrong parameters\n"); return 0;};
buffer = fill_buffer(argv[1]);
str = strtok(buffer, DELIMITERS);
if ((fr = fopen(argv[2], "w+")) == NULL)
{
printf("failed to open file for writing\n");
return 0;
};
while (str)
{
temp = str;
last_char = '\0';
while (*temp)
{
last_char = *(temp);
temp++;
}
//everything works about until here...
if (!full()) push(str);
str = strtok(NULL, DELIMITERS);
}
// butt he real problem appears in here when i get an error.. somehow connected to
//the string types %s &g or something.. no idea?????
while (!empty())
fprintf(fr,"%s\n", pop()); //Why?!!!....
return 0;
}
data.txt
aaaaaa
bbbbbb
ccccccc
result.txt (error in here...)
aaaaaa
bbbbbb
ccccccc