Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,489 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,288 people online right now. Registration is fast and FREE... Join Now!




input and output of text file

 
Reply to this topicStart new topic

input and output of text file, each line of input file must be <= 40 chars

rizwans
27 Feb, 2007 - 06:37 PM
Post #1

New D.I.C Head
*

Joined: 26 Oct, 2005
Posts: 25


My Contributions
Hi All

I have a bit of a problem here. I am trying to read in a text file and output its contents to the screen. I don't have to output it to another file. I already have the file input and output read in, but the problem I'm having is that each line of the input file is less than 40 chars in length and its output must be 40 chars in length for each line. Once 40 chars are output, it must goto the nextline.

Here's an example of what i mean:

Hi there,
I am here. this line
is less
than 40 chars, isn't it?
.
hello
should look like:

Hi there, I am here. this line is less than 40 char
s, isn't it?. hello

0123456789012345678901234567890123456789


code so far:
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
ifstream inputfile;
char line[40];

inputfile.open("text.txt", ios::in);
inputfile.get(line,40)

if (inputfile.fail())
{
cout << "Error opening file";
}

else
{
cout << "got file";
cout << line;
}



return 0;
}

I know I have to have an if statement in my else such as
if line > 40 chars
goto new line

but not too sure how to implement.

By the way I can't use any other functions other than ifstream::eof, ifstream::get, cout.put, cout << "..".

The reason is that I have to translate it to assembly language later.

Thanks for the help
Riz
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Input And Output Of Text File
27 Feb, 2007 - 09:06 PM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,153



Thanked: 44 times
Dream Kudos: 125
My Contributions
Well, one thing you can do is instead of printing line by line from file
try to print it character by character. This way all you need to do is
just check for 40 characters each line on screen and then add newline
character.

And you can have a check on escape characters that are coming from
file (ex. '\n') and don't print them.

Did you get my point? huh.gif


User is offlineProfile CardPM
+Quote Post

rizwans
RE: Input And Output Of Text File
27 Feb, 2007 - 09:19 PM
Post #3

New D.I.C Head
*

Joined: 26 Oct, 2005
Posts: 25


My Contributions
QUOTE(AmitTheInfinity @ 27 Feb, 2007 - 10:06 PM) *

Well, one thing you can do is instead of printing line by line from file
try to print it character by character. This way all you need to do is
just check for 40 characters each line on screen and then add newline
character.

And you can have a check on escape characters that are coming from
file (ex. '\n') and don't print them.

Did you get my point? huh.gif



I think i did. I'm just trying to figure out the code now for this.
I'll come back in a bit once i figure it out. I'm constantly checking this post.
thanks a lot though. any bit does help
User is offlineProfile CardPM
+Quote Post

rizwans
RE: Input And Output Of Text File
27 Feb, 2007 - 09:28 PM
Post #4

New D.I.C Head
*

Joined: 26 Oct, 2005
Posts: 25


My Contributions
i forgot to mention that the text must be held within an array of size 40 because the width must always be interchangeable.

ie:
char line[40]
or
char line[10]

User is offlineProfile CardPM
+Quote Post

rizwans
RE: Input And Output Of Text File
27 Feb, 2007 - 09:53 PM
Post #5

New D.I.C Head
*

Joined: 26 Oct, 2005
Posts: 25


My Contributions
i tried it and i couldn't do it.

i know i'm doing something wrong just can't figure it out.
this is what i tried though:

char line[40];
char ch;
if (inputfile.fail())
{
cout << "error";
}

else
while(inputfile.get(ch))
{
if (ch == line); //error
cout << '\n';
}

error:
c++ forbids comparison between pointer and integer
also, how do i check for end of file
I tried doing:
if (inputfile.fail() || inputfile.eof())
and it gave me a whackload of errors.

I read in a book that it should be:
while (!inputfile.eof())
{
//process
}do

I have tried incorporating that into the program but i also need the process
which is the main problem, before I can do the check for eof.
Please help
I've been at this for hours and hours.
User is offlineProfile CardPM
+Quote Post

rizwans
RE: Input And Output Of Text File
27 Feb, 2007 - 10:17 PM
Post #6

New D.I.C Head
*

Joined: 26 Oct, 2005
Posts: 25


My Contributions
I still can't get this. I have tried everything i can possibly think of!
Now I have an infinite loop when i add the eof in a while statement.

I hate C++. I feel like throwing my little iBook G4 across the room and smashing it. I found a c++ cookbook that has the code in there, but it uses the strings class and that is not allowed.

Computer Science: 2 thumbs down
Throwing my laptop: 2 thumbs up

I'm going for a cigarette, I'll be back
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Input And Output Of Text File
27 Feb, 2007 - 10:45 PM
Post #7

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,153



Thanked: 44 times
Dream Kudos: 125
My Contributions
biggrin.gif Well don't get sooo angry...
I said just count characters while you are printing on screen
and avoid the escape characters you get from file.

And is it compulsary for you to have it in array? I can't find any reason to have array to handle line length. But still if you want it with array then store those characters one by one in array instead of printing and print whole array when you reach the length

It's something like...

CODE


char ch;
int count =0;
int length = 40; // you can change your line length here.

while(!inputfile.eof()) //check for end of file
{
  if(count>length) //  line length reached. go to new line, reset counter.
  {
    count =0;
    cout<<"\n";
  }
  inputfile.get(ch); //get a character
  if(ch!='\n' && ch!='\t') //check it's not an escape character
  {
    count++; //character to be printed increment count.
    cout<<ch; //print it.
  }
}



ignore any syntax errors there and try to get the logic of printing...
some adjustments here and there might be needed as I didn't tried to
run it...
It's all about cool mind... cool.gif Think on it and you will get it.

hope this will hope you... smile.gif

This post has been edited by AmitTheInfinity: 27 Feb, 2007 - 11:30 PM
User is offlineProfile CardPM
+Quote Post

rizwans
RE: Input And Output Of Text File
27 Feb, 2007 - 11:36 PM
Post #8

New D.I.C Head
*

Joined: 26 Oct, 2005
Posts: 25


My Contributions
WOW!!!!

You are the greatest programmer alive. I figured it out and it works. It works amazingly!!! Thank you Thank you!!!

If you lived in calgary i'd buy you a beer if you drink!!!

WOW
THANK YOU

I didn't use the array because i read the question wrong. just had to be a constant not array. It's what happens after 7 hours in front of a computer. Now onto Binary Trees.


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 05:15PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month