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

items should be an array of pointers, in this program, the chars are stored in item & not the pointers,

char *items[10] ; should be used & in push just save, items[top++] = c;
& similarly change, the pop()

And also, fns defined inside main?

char *items[10]; should be used & in push just save, items[top++] = c;
& similarly change, the pop() // you mean something like this:

//==============================================================================
char *items[10];

void push(char *c) {

    items[++top] = c;
}

char pop() {
//what to change here exactly ?

   return  items[top--];

}
//==============================================================================

And also, fns defined inside main? what is fns?

char *pop( ) { return items[top--] } ;
This is what I ment, & you code, compiled & executed well.

this works :)
but still, when i put this part of the code to the full program the results are a bit strange..
lets say we have 4 lines:
asasasasas
aaaa
bbbb
cccc

but the program only prints:
cccc
cccc
cccc
cccc

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSTACK 10
#define EMPTYSTACK -1
//------------------------------------------------------------------------------
void AD(char * record, char * record_out)
{
    char *p1 = record;
    char *p2 = record_out;

     while( *p1 )
    {
        // use temp pointer to locate
        // end of digits
        char* p3 = p1;
        while( isdigit(*p3) )
            p3++;
        // is there a space following the digits ?
        if( *p3 == ' ')
            // yes, then skip the space and move on to the next char
            p1 = p3+1;
        else
        {
            // no, then was the last char put into temp buffer
            // a space
            if( *(p2-1) == ' ')
                // yes, then overwrite that space with a new character
                p2--;
            p1 = p3;
        }
        // copy all characters up to the next digit
        while(*p1 && !isdigit(*p1) )
            *p2++ = *p1++;
    }
}
//------------------------------------------------------------------------------
int count (char vector[],int nr)

{
    int gasit=0,i; 
    for(i=0;i<nr;i++)
    {
        if(vector[i]>='a' && vector[i]<='z') gasit++;
          if(vector[i]>='A' && vector[i]<='Z') gasit++;
    }
   
    return gasit;

}
//------------------------------------------------------------------------------
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[10];

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,*t;
	char record[100];
	char last_char;
	int kiek,ln;
	FILE *fr;
    HOW=0;
	if (argc != 3) {printf ("wrong parameters\n"); return 0;};
	buffer = fill_buffer(argv[1]);	
	str = strtok(buffer, DELIMITERS);
	
	if ((fr = fopen(argv[2], "w+")) == NULL) 
	{
		printf("mistake: can't open file for writing\n");
		return 0;
	};

	while (str)
		{
			temp = str;
			last_char = '\0';
			while (*temp)
				{
					last_char = *(temp);
				    temp++;
				}
								
       char recordd[sizeof(record)] = {0}; 
       AD(str,recordd);             
       ln = strlen(recordd)-1;
       HOW = ln-count(recordd,ln) +HOW+1;
       
t=recordd;
printf("%s\n",t);

 if (!full()) push(t);

		str = strtok(NULL, DELIMITERS);
		}	
// Why does program print just the last line here repeatedly?
   while (!empty())
      fprintf(fr,"%s\n", pop());	
		
	fprintf(fr,"\n");
	fprintf(fr,"Number of words: %d\n",HOW);
	return 0;
}

I modified the code, without changing the structure much.
And it worked. Some functions looked meaningless to me & so I removed them.

Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<ctype.h>
#define MAXSTACK 10
#define EMPTYSTACK -1

char *fill_buffer(char *);
void push( char* );
char *pop();
int empty();
int full();  

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 top = EMPTYSTACK;
char *items[10];

void push(char *c) {
    items[++top] = c;
}

char *pop( )  {  
   return  items[top--];   
}

int full()  {
   return top+1 == MAXSTACK;
}

int empty()  {
   return top == EMPTYSTACK;
}
    
int main(int argc, char **argv)
{
                char *DELIMITERS = "\n\r 1234567890";  // check for digits & space
	char *str, *buffer, *temp,*t;
	char record[100];
	char last_char;
	int kiek,HOW = 0;
 	FILE *fr;
	if (argc != 3) {printf ("wrong parameters\n"); return 0;};
	buffer = fill_buffer(argv[1]);	
	str = strtok(buffer, DELIMITERS);

	if ((fr = fopen(argv[2], "w")) == NULL) 
	{
		printf("mistake: can't open file for writing\n");
		return 0;
	};

	while (str)
	{
                        if (!full()) push(str);
                        HOW++ ;
	        str = strtok(NULL, DELIMITERS);
                 }	

                 while (!empty())
                       fprintf(fr,"%s\n", pop());	
	 fprintf(fr,"\n");
	 fprintf(fr,"Number of words: %d\n",HOW);
	 return 0;
}

And thanks for the comment

Well thanks , for the code it's great ;)
But there is a slight problem about it ....
I probably didn't said what the program should do, so:
firstly, we take a lines from file:

2323big 333house 3333 bi 332323g
333hhhh hhhh ddd 33f
....
then transform them with("meaningless function") :

big house big (changing numbers to symbols which were before numbers)
hhhh hhhh dddf
...
count words in line("mf")...
then move lines to stack
then print to file from stack lines
big house big (line1)
hhhh hhhh dddf (line2)
etc.
(my code just prints repeatedly the last line):
hhhh hhhh dddf
hhhh hhhh dddf
ideas?

I'll reply a little later & then I didnt mean to hurt, sorry if I did.

And I have a strange question. What compiler do you use? Cause I would always be promted by the complier If I DEFINE a fn inside another fn.

Well I use cygwin>gcc -o programname main.c
for writing code dev-C++
PS. ; Don't worry I'm fine

Well, I guess this should me the final modification!!!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define isdigit(d) ((d)>47 && (d)<58) ? 1 : 0
#define isalpha(d) (((d)>='A' && (d)<='Z') || ((d)>='a' && (d)<='z'))?1:0
#define MAXSTACK 10
#define EMPTYSTACK -1
//------------------------------------------------------------------------------
void AD(char * record, char * record_out)
{
    char *p1 = record, *p2 =  record_out;
    while( *p1 != '\r' ) 
    {
       if ( isdigit(*p1) ) {
            p1++ ;
            continue ;
       }
       *record_out++ = *p1++;
    }
    *record_out++ == *p1 ;
    *record_out = '\0' ;
}
//------------------------------------------------------------------------------
int count (char vector[],int nr)

{
    int gasit=0,i; 
    for(i=0;i<nr;i++)
        if ( isalpha ( vector[i] ) ) gasit++ ;
    return gasit;

}
//------------------------------------------------------------------------------
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;
};
char *items[10];
int top = EMPTYSTACK;
void push( char* );
char *pop();
int empty();
int full(); 
//==========================================================
void push(char *c) {
    items[++top] = c;
}

char *pop( )  {  
   return  items[top--];   
}


int full()  {
   return top+1 == MAXSTACK;
}

int empty()  {
   return top == EMPTYSTACK;
}
//------------------------------------------------------------------------------

int main(int argc, char **argv)
{
    char *DELIMITERS = "\n";  
	char *str, *buffer, *temp,*t;
	char record[100];
	char last_char;
	int kiek,ln;
	FILE *fr;
    int HOW=0;
	if (argc != 3) {printf ("wrong parameters\n"); return 0;};
	buffer = fill_buffer(argv[1]);	
	str = strtok(buffer, DELIMITERS);
	
	if ((fr = fopen(argv[2], "w+")) == NULL) 
	{
		printf("mistake: can't open file for writing\n");
		return 0;
	};

	while (str)
	{
		temp = str;
		last_char = '\0';
		while (*temp)
		{
           last_char = *(temp);
	       temp++;
        }
		AD(str,record);             
        ln = strlen(record);
        HOW += ln-count(record,ln);
       
        printf(record);

        if (!full()) push(record);
		str = strtok(NULL, DELIMITERS);
	}	

    while (!empty())
      fprintf(fr,"%s\n", pop());	
		
	fprintf(fr,"\n");
	fprintf(fr,"Number of words: %d\n",HOW);
	return 0;
}

Well, I guess this should me the final modification
Well thanks for the code again it really works, unfortunately there are two slight problems about it :(

firstly, hmmm few posts back I wrote that program prints lines to file incorrectly
(I don't know if it is in your pc as well?.... )
that means if we have ten lines in data.txt :
qqqq ddd
aaa dd
a ds
as
....
A

then the program will print to the result.txt
A (last line ,transformed)
A
A
A
...
A
words: 10

program to the monitor will print changed lines but not to the file;

Secondly, the lines are being changed not in the right way,
because this script just deletes number
f.e.:

3333ddd 33noway dddd noway

It should change this like this : ddddnoway
number in front deleted, and the word with the number in front of it, "gets" ddd

or f.e. 333bi 44444g 33333 hou 444se
changed: big house

finally, my program about five posts back,sorry for irritating, did line transformation right
but it couldn't print transformed lines from stack to FILE.TXT it was just printing last line like 20 times.... same problem with this program(I hope you have it also), and in my program was something wrong with char recordd[sizeof(record)] = {0}; if I changed it , program just started to print unclear symbols, or don't work...
PS. sorry for not explaining everything right in the first post...

misake:
...because this script just deletes number
f.e.:

3333ddd 33noway dddd noway

It should change this like this : dddnoway dddd noway...

<<then the program will print to the result.txt A (last line ,transformed)
The reason why this happens is that the push() uses everytime the same buffer (=record), so effectively the stack contains pointers to this single buffer, and what was last written to the buffer, gets printed to the file as many times as pop() is called.

so , how exactly to change it?
make something, like an array?

<<then the program will print to the result.txt A (last line ,transformed)
The reason why this happens is that the push() uses everytime the same buffer (=record), so effectively the stack contains pointers to this single buffer, and what was last written to the buffer, gets printed to the file as many times as pop() is called.

Ya, he's correct, I missed it completly, perhaps use dynamic allocation. That should work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.