Jump to content

Buffer

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
16 replies to this topic

#1
Japie

Japie

    Newbie

  • Members
  • Pip
  • 9 posts
I have to read in a file and print it in reverse order, using buffers.
So I read a file and put it into a char *buffer and now I can print by printf(buffer). So far so good.
But how can I acces the data in the buffer to reverse it?

And second question. Can I use a stack to put my buffers in and pop it out so LIFO?

#2
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
You can use the buffer explicitly, but you can also use a stack.

To access the buffer from behind, you could use strlen() as one alternative.
int Index;
char *Buffer = "Hello, World!";
	
for(Index = strlen(Buffer)-1; Index >= 0; Index--)
	putchar(*(Buffer + Index));
There's other ways as well.

#3
limo

limo

    Newbie

  • Members
  • PipPip
  • 14 posts

Japie said:

I have to read in a file and print it in reverse order, using buffers.
So I read a file and put it into a char *buffer and now I can print by printf(buffer). So far so good.
But how can I acces the data in the buffer to reverse it?

And second question. Can I use a stack to put my buffers in and pop it out so LIFO?

Well yes you could use a stack but why would you? A simple while loop will allow you to reverse the contents of the buffer.

int lengthOfString = strLen(yourCharBuffer);

int midIndex = lengthOfString/2;

int startIndex = 0;

int endIndex = lengthOfString - 1;

while (startIndex < midIndex)

{

  char rightElement = yourCharBuffer[endIndex];

  yourCharBuffer[endIndex] = yourCharBuffer[startIndex];

  yourCharBuffer[startIndex] = rightCharElement;

  startIndex++;

  endIndex--;

}

yourCharBuffer is now reversed

#4
Japie

Japie

    Newbie

  • Members
  • Pip
  • 9 posts
Thank you, it does work :)

The reason I was talking about stacks is that eventually more buffers must be stored in a data structure (stack) until end of file and then they must be popped out again and be printed on the screen.

#5
limo

limo

    Newbie

  • Members
  • PipPip
  • 14 posts

Japie said:

Thank you, it does work :) The reason I was talking about stacks is that eventually more buffers must be stored in a data structure (stack) until end of file and then they must be popped out again and be printed on the screen.

Of course it does. ;) Yes you can use a stack, but if you already know the size of the buffer beforehand, then there's no need to use one. Anyway here's an algorithm for a stack implementation.


    stack<char> allchars; // stack of all chars

    int charIndex = 0;

    while (yourBuffer[charIndex++] != null) { // push if there's stuff to read

        allchars.push(yourBuffer[charIndex-1]);

    }


    while (!allwords.empty()) {

       char topChar = allchars.top();

       cout << topChar; // print out topChar

       allwords.pop(); // pop the top element

    }


#6
Japie

Japie

    Newbie

  • Members
  • Pip
  • 9 posts
If I want to make a stack of buffers is it then required to make a stack with nodes of type char [] or should I make a stack of pointers that refer to those buffers?

#7
kkelly

kkelly

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
A recursive algorithm may be the simplest solution.

#8
limo

limo

    Newbie

  • Members
  • PipPip
  • 14 posts
Are you trying to store chars, or char arrays? Just initialise your stack depending on what you want to store, char or char*. You can use recursion also, although depending on the amount of stack space you have you could encounter a stack overflow.

#9
Japie

Japie

    Newbie

  • Members
  • Pip
  • 9 posts
Creating a stack was not as easy as I thought.
Anyway, I have created a push-function and pop-function. Since I am not good at C I decided to use less parameters so I save my stack in a global. My functions are now like push(char *buffer) and pop() instead of push(Stack *s, char *buffer) and pop(Stack *s).

Is that a good idea...or...is it even possible, because my stack does save some data, but pops wrong things in different orders :(

#10
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
It's possible, and it's not a bad idea.

I don't think there's anything wrong in using global variables, though I prefer not to use them myself. You're may declaring the global variable wrong, if the pop goes wrong. Check this, for how to declare and define global variables right in C.

#11
Japie

Japie

    Newbie

  • Members
  • Pip
  • 9 posts
I have created a pushStack function now but it doesnt work at all.

This is the code:

void pushStack(char *buffer){

struct Stack *temp;
temp = malloc(sizeof(*temp));
temp -> data = buffer;
temp -> next = s;
s = temp;
}

I call this function in the Main function. The problem is that when I
read a second buffer the data of s will change at the same moment and I have lost the first buffer. Probably because s - > data refers to the memory address of buffer. How can I solve this?
By the way...the variable s is a global variable of type Stack.

#12
kkelly

kkelly

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
If this helps, here is a recursive solution:
#include <iostream>

#include <fstream>


using namespace std;


bool PushAndPop(char SomeChar);


ifstream myFile("testing.txt");


int main()

{

	if (!myFile)

	{

		cout << "No file" << endl;

	}

	else

	{

		cout << "Reading file . . ." << endl;

		PushAndPop(EOF);

	}


	myFile.close();


	cin.get();


	return 0;

}

bool PushAndPop(char SomeChar)

{

	SomeChar = myFile.get();


	if (SomeChar == EOF)

	{

		return true;

	}

	else

	{

		if (PushAndPop(SomeChar))

		{

			cout << SomeChar;

		}

	}

}

I don't have time to add comments, but it is simple to follow.