Skip to content

Commit 21cc96e

Browse files
committed
introduces header filecheck for functions on previous commit. Adds function that counts characters in a file
1 parent d00eec0 commit 21cc96e

File tree

3 files changed

+108
-67
lines changed

3 files changed

+108
-67
lines changed
Lines changed: 6 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,23 @@
1-
/* Program commented.cpp outputs itself without comments */
1+
/* Program to try out the functions in filecheck.cpp */
22

33
#include<iostream>
44
#include<fstream>
55
#include<string>
6+
#include"filecheck.h"
67

78
using namespace std;
89

9-
void print_with_comments(ifstream& in_stream);
10-
void print_without_comments(ifstream& in_stream, ofstream& out_stream);
11-
1210
/* Start of main program */
1311
int main() {
1412
ifstream in_stream;
1513
ofstream out_stream;
1614

1715
cout << "Testing: " << 16/2 << " = " << 4*2 << ".\n\n";
18-
//in_stream.open("commented.cpp");
19-
//print_with_comments(in_stream); // closes file
20-
2116
in_stream.open("commented.cpp");
22-
out_stream.open("uncommented.cpp");
23-
print_without_comments(in_stream, out_stream);
24-
}
25-
26-
/* Function that prints file contents to screen */
27-
void print_with_comments(ifstream& in_stream){
28-
char character;
29-
30-
in_stream.get(character);
31-
32-
while(!in_stream.eof())
33-
{
34-
cout << character;
35-
in_stream.get(character);
36-
}
37-
in_stream.close();
38-
}
39-
40-
/* Function that prints file contents to a file*/
41-
void print_without_comments(ifstream& in_stream, ofstream& out_stream){
42-
const int False = 0;
43-
const int True = 1;
44-
char character;
45-
char next_character;
46-
int inside_comment = False;
47-
48-
in_stream.get(character);
49-
50-
while(!in_stream.eof())
51-
{
52-
/* Check if there's a comment */
53-
if(inside_comment == False && character == '/')
54-
{
55-
in_stream.get(next_character);
56-
if(next_character=='*')
57-
inside_comment = True;
58-
else
59-
in_stream.putback(next_character);
60-
}
61-
62-
/* Check if at the end of a comment */
63-
if(inside_comment == True && character == '*')
64-
{
65-
in_stream.get(next_character);
66-
if(next_character == '/')
67-
in_stream.get(character);
68-
inside_comment = False;
69-
70-
}
71-
72-
73-
/* Write to file and screen */
74-
if(inside_comment == False){
75-
out_stream.put(character);
76-
cout << character;
77-
}
78-
79-
in_stream.get(character);
80-
}
81-
in_stream.close();
17+
count_characters(in_stream); // closes file
8218

19+
// in_stream.open("commented.cpp");
20+
// out_stream.open("uncommented.cpp");
21+
// print_without_comments(in_stream, out_stream);
8322
}
8423

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include<iostream>
2+
#include<fstream>
3+
#include<string>
4+
#include"filecheck.h"
5+
6+
using namespace std;
7+
8+
/* Function to count the number of characters in a file (including blanks
9+
and comments)*/
10+
void count_characters(std::ifstream& in_stream){
11+
char character;
12+
int count = 0;
13+
14+
in_stream.get(character);
15+
while(!in_stream.eof())
16+
{
17+
count++;
18+
in_stream.get(character);
19+
}
20+
in_stream.close();
21+
22+
cout << "There are " << count << " characters in the file. \n \n";
23+
24+
}
25+
26+
/* Function that prints file contents to the screen*/
27+
void print_with_comments(ifstream& in_stream){
28+
char character;
29+
30+
in_stream.get(character);
31+
while(!in_stream.eof())
32+
{
33+
cout << character;
34+
in_stream.get(character);
35+
}
36+
in_stream.close();
37+
}
38+
39+
/* Function that prints file contents without comments in / * to a file*/
40+
void print_without_comments(ifstream& in_stream, ofstream& out_stream){
41+
const int False = 0;
42+
const int True = 1;
43+
char character;
44+
char next_character;
45+
int inside_comment = False;
46+
47+
in_stream.get(character);
48+
49+
while(!in_stream.eof())
50+
{
51+
/* Check if there's a comment */
52+
if(inside_comment == False && character == '/')
53+
{
54+
in_stream.get(next_character);
55+
if(next_character=='*')
56+
inside_comment = True;
57+
else
58+
in_stream.putback(next_character);
59+
}
60+
61+
/* Check if at the end of a comment */
62+
if(inside_comment == True && character == '*')
63+
{
64+
in_stream.get(next_character);
65+
if(next_character == '/')
66+
in_stream.get(character);
67+
inside_comment = False;
68+
}
69+
70+
71+
/* Write to file and screen */
72+
if(inside_comment == False){
73+
out_stream.put(character);
74+
cout << character;
75+
}
76+
77+
in_stream.get(character);
78+
}
79+
in_stream.close();
80+
81+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef FILECHECK_H
2+
#define FILECHECK_H
3+
4+
#include <fstream>
5+
using namespace std;
6+
7+
8+
/* Function to count the number of characters in a file (including blanks
9+
and comments)*/
10+
void count_characters(ifstream& in_stream);
11+
12+
13+
/* Function that prints file contents to the screen*/
14+
void print_with_comments(ifstream& in_stream);
15+
16+
17+
/* Function that prints file contents without comments in / * to a file*/
18+
void print_without_comments(ifstream& in_stream, ofstream& out_stream);
19+
20+
21+
#endif

0 commit comments

Comments
 (0)