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
Solved

Determining when end of line (of file) reached

Posted on 2000-02-28
6
594 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

  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 …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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…
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.

809 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