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?
Buffer
Started by Japie, Oct 23 2007 05:26 AM
16 replies to this topic
#1
Posted 23 October 2007 - 05:26 AM
|
|
|
#2
Posted 23 October 2007 - 05:56 AM
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.
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
Posted 23 October 2007 - 06:00 AM
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?
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
Posted 23 October 2007 - 06:36 AM
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.
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
Posted 23 October 2007 - 06:58 AM
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
Posted 24 October 2007 - 06:30 AM
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
Posted 24 October 2007 - 10:00 AM
A recursive algorithm may be the simplest solution.
#8
Posted 24 October 2007 - 04:48 PM
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
Posted 27 October 2007 - 03:49 PM
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 :(
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
Posted 27 October 2007 - 11:38 PM
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.
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
Posted 30 October 2007 - 06:03 AM
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.
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
Posted 30 October 2007 - 02:29 PM
If this helps, here is a recursive solution:
I don't have time to add comments, but it is simple to follow.
#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.


Sign In
Create Account

Back to top









