Dec 8, 2012 at 1:24pm
i dont know how to create an instream file that can be readable by c++
and how can i know when to use ifstream and ofstream
Dec 8, 2012 at 1:36pm
Last edited on Dec 8, 2012 at 1:36pm
Dec 8, 2012 at 1:48pm
@ Peter87 Thanks for the clarification. I was trying to keep things very simple, but sometimes that can be misleading too. :)
Dec 8, 2012 at 1:56pm
and how to create file that c++ can read (like a txt file)
do lots of ways and still fails
ex :
the contain of file :
When I see your face
After running the program, it shows:
Filename: 1.txt
The content:
When I see your face
and my c++ program :
#include <iostream.h>
#include <fstream.h>
#include <assert.h>
#include <windows.h>
#include <conio.h>
#include <stdio.h>
typedef struct Node
{
char character[80];
Node *next, *pre;
}Line;
Line *currentline;
Line *firstline;
Node *head, *tail;
int col;
int row;
void createfirstline()
{
Node *p;
p = new Node;
currentline = p;
head = currentline;
tail = currentline;
}
void newline()
{
Node *p;
p = new Node;
p -> next = NULL;
p->pre = currentline;
currentline->next = p;
tail = p;
currentline=p;
}
void readfile()
{
Node *p;
p = head;
if (head == NULL)
cout << "\nFile is empty!\n" << endl;
else
{
cout << "\nThe content: " << endl << endl;
while (p != NULL)
{
cout << p->character;
p = p->next;
}
}
}
void gotoxy()
{
int x;
int y;
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void main()
{
cout << "Filename: ";
char filename[30];
cin.getline (filename,30);
ifstream instream;
instream.open(filename,ios::in);
if (!instream)
{
cout << "Cannot open the file\n";
}
else
{
char reading;
currentline = firstline;
createfirstline();
while(instream.read(&reading,sizeof(reading)))
{
if (reading == '\n')
newline();
else
{
col++;
currentline->character[col] = reading;
char input;
do
{
input = getch();
switch (input)
{
case 72:
row--;
continue;
case 80:
row++;
continue;
case 77:
col++;
continue;
case 75:
col--;
continue;
}
gotoxy(col,row);
}
while (input != 13);
}
}
}
readfile();
instream.close();
}
can you explain more details about ifstream and ofstream, it still hard for me to understand, what difference between new file and existing file
Last edited on Dec 8, 2012 at 3:33pm