Solved

Determining when end of line (of file) reached

Posted on 2000-02-28
6
597 Views
Last Modified: 2006-11-17
I'm trying to write a prog that takes a file called censor.in with the 1st line containing the number of subsequent lines and outputting it into a file called censor.out with all the four-letter words *'ed out.

Following is how the input file would look:

2
The quick brown fox jumps over the lazy dog
Now is the time for all good people to come to the aid of the party

Here's the code I have so far:

#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

void main() {

      int n;
      int count = 0;
      char str[20];

      ofstream out("censor.out");
      ifstream in("censor.in");

      in>>n;

      while (in) {
            in>>str;
            count++;
      }

      in.close();
      ifstream in2("censor.in");

      in2>>n;

      while (count != 1) {
            in2>>str;
            if (toupper(*str) == *str) {
                  out<<"\n\n";
            }
            if (strlen(str) == 4) {
                  out<<"**** ";
            } else {
                  out<<str<<" ";
            }
            count = count - 1;
      }

      in2.close();
      out.close();
      
      cout<<"Have a good day\n\n";

}

As you can see, I just take the integer out of the picture and deal with each of the words.  I need a better way to determine when I new line begins though by counting the number of "str" values before a line break.  In the example I just said "When there's a capital letter break to the next line" but names, etc. would mess that logic up.  Any ideas?  Thanks a lot.
0
Comment
Question by:jkeebler
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 24

Expert Comment

by:SunBow
ID: 2567043
I am not sure what you want. Is each sentence fully contained on a line? Rather than depend on CAP to begin a sentence, you could try period to end. (but if numbers include non-integer, you'll get the same problem). If string ends in null, or lf or cr or crlf, they could be searched for, or you could count the chars per line in advance, or do a fetch of one line at a time.

That's some ideas, but if you do not create the input file, I'd recommend going for the line termination method used by the one that created the file.
0
 

Author Comment

by:jkeebler
ID: 2567054
There is nothing at the end of each line but a hard return.  Is there any way to detect that?

BTW, I'm 3083744 on ICQ.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2567239
We cannot provide answers to school assignments.  That is grounds for removal from this site.  (for both you and the experts involved.)  We can provide only limitied help in accademic assignments.    We can answer specific (direct) questions, like you might ask your teacher.  We can review your work and post suggestions, again, like your teacher might do.

Do you have specific questions?
Do you have any work on this (incomplete even) that we can review?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jkeebler
ID: 2567322
nietod,
    In regards to your question, "Do you have any work on this (incomplete even) that we can review?": I posted what I have already in the initial question.
    Also, this is NOT a school assignment. It is a practise question from a contest (http://www.math.uwaterloo.ca/~ccc/1998/1a-prob.html
).  I simply wanted to know any other ways to detect the end of a line of text in a file.  I'm sorry if this conflicts with your bots.

jkeebler
0
 
LVL 3

Accepted Solution

by:
LucHoltkamp earned 75 total points
ID: 2567938
use in.getline() (look it up in the help) to grab a line at the time from the file.

Next parse every line to break it up in words, a word is terminated by whitespace or the end of the string:

Next will be inside the loop:


const int bufferSize = 256;
char buffer[bufferSize];
in.getline(buffer, bufferSize, '\n');

// next get the words
int i = 0, j = 0;
char word[bufferSize];
while (buffer[i])
{
  if (!isspace(buffer[i])
    word[j++] = buffer[i++];
  else
  {
     if (j)
     {
        if (j == 4) out << "****";
        else
        {
           word[j] = '\0';
           cout << word;
        }
        j = 0;
     }    
     out << buffer[i++];
  }
}

hope this helps, if you have any questions, ask...
Luc
0
 
LVL 24

Expert Comment

by:SunBow
ID: 2574487
>> lines = atoi(ipt.readline())
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question