Solved

Determining when end of line (of file) reached

Posted on 2000-02-28
6
592 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now