Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Determining when end of line (of file) reached

Posted on 2000-02-28
6
Medium Priority
?
600 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
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 300 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

926 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