Solved

Determining when end of line (of file) reached

Posted on 2000-02-28
6
591 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:jkeebler
Comment Utility
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
Comment Utility
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
Comment Utility
>> lines = atoi(ipt.readline())
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

744 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

17 Experts available now in Live!

Get 1:1 Help Now