how do you read in a text file char by char a store in a string?
coolmel55 0 Light Poster
How do I find for example ; in the file now?
/* Contains implementation of all functions associated with the Lines Class */
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include "loc.h"
using namespace std;
//Constructor, initializes necessary variables
Lines::Lines()
{
}
//Requests a filename from the user
void Lines::count_lines()
{
char filename[16];
int num_lines = 0;
char get_next_line[200];
cout << "Enter the name of the program that you would like data for: ";
cin >> filename;
fstream instream(filename);
instream.getline(get_next_line, 300);
if(instream.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
while (! instream.eof()) //checks for end of file
{
if ((get_next_line[0])== '#')
{
//checks for includes and ifndef, define, endif
num_lines++;
}
else
{
}
instream.getline(get_next_line, 300);
}
cout << "There are " << num_lines << " lines." << endl << endl ;
instream.close();
}
Dave Sinkula 2,398 long time no c Team Colleague
>How do I find for example ; in the file now?
#include <iostream>
#include <string>
int main()
{
std::string line;
while ( getline(std::cin, line) )
{
std::string::size_type pos = line.find(";");
if ( pos != std::string::npos )
{
std::cout << line << std::endl;
}
}
return 0;
}
Feeding this source file to its resulting executable produces this output.
std::string line;
std::string::size_type pos = line.find(";");
std::cout << line << std::endl;
return 0;
coolmel55 0 Light Poster
Could you explain what you did to me as I don't understand completely?
Stack Overflow 8 Junior Poster
Greetings,
Since you are using the C++ language, I will stick with the syntax. A good way to read a stream character by character would be a function called sgetc():
What is sgetc()?
int sgetc( );
sgetc() is a function that returns the character at current get position, or EOF if the get position is the end of the input sequence.
For example, if you wanted to read each and every letter before EOF, you could easily use an if-else statement for call checking.
#include <iostream>
#include <fstream>
using namespace std;
int main () {
char ch;
streambuf *pbuf;
ifstream instr ("myFile.txt"); // Your file
// Get streambuf object associated with stream
pbuf = instr.rdbuf();
// Time to go through the stream
while (pbuf->sgetc() != EOF) {
// Get current character
ch = pbuf->sbumpc();
// Call checking here [[b]if-else[/b]], etc..
cout << ch; // Display
}
instr.close(); // Close file
return 0;
}
Code 1.1: Read stream one character at a time.
What is rdbuf()?
rdbuf() gets/sets the streambuf object associated with the stream.
What is sbumpc()?
sbumpc() is a function that returns the character at the current get position, and then increases the get pointer by one unless the get position is at the end of the input sequence, in which case EOF is returned.
You can learn more about streambuf, and it's functions here.
Hope this helps,
- Stack Overflow
coolmel55 0 Light Poster
Ok soo it appears that I have to change my whole program as I'm off track right?
It bums me out that I spend time and then have to start over!
Stack Overflow 8 Junior Poster
You can always do it the default way. Dave presented a pretty good example how to do it with iostream, though I just used streambuf for simplicity. If you'd like, I can explain what his code did and how it works.
- Stack Overflow
coolmel55 0 Light Poster
I would definately appreciate the explaination, it's one thing to copy and paste but I need to understand.
Stack Overflow 8 Junior Poster
Alright, lets disect this one line at a time.
We already know about iostream, so lets skip the first line.
» #include <string>
This is the standard header <string> to define the container template class basic_string and various supporting templates.
» std::string line;
Not using the namespace std, we must declare what string is hence the std::string. If you did use the namespace, you wouldn't have to declare where string lies. A namespace is like a context which determines the meaning of a symbol (if you think of it as a space where names are stored, you won't be too far wrong).
» while ( getline(std::cin, line) )
As we may know getline(), it simply gets a line from the stream.
» std::string::size_type pos = line.find(";");
size_type
typedef T2 size_type;
The unsigned integer type describes an object that can represent the length of any controlled sequence. It is described here as a synonym for the implementation-defined type T2.
find
The member functions each find the first (lowest beginning position) subsequence in the controlled sequence, beginning on or after position off, that matches the operand sequence specified by the remaining operands. If it succeeds, it returns the position where the matching subsequence begins. Otherwise, the function returns npos.
» if ( pos != std::string::npos )
npos
static const size_type npos = -1;
The constant is the largest representable value of type size_type. It is assuredly larger than max_size(), hence it serves as either a very large value or as a special code.
Hope this helps,
- Stack Overflow
coolmel55 0 Light Poster
Stuck again!! I'm going to quit soon!
/* Contains implementation of all functions associated with the Lines Class */
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include "loc.h"
using namespace std;
//Constructor, initializes necessary variables
Lines::Lines()
{
}
//Requests a filename from the user
void Lines::count_lines()
{
char filename[16];
int num_lines = 0;
char get_next_line[300];
std::string line;
cout << "Enter the name of the program that you would like data for: ";
cin >> filename;
fstream instream(filename);
instream.getline(get_next_line, 300);
if(instream.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
while ( getline(std::cin, line))
{
std::string::size_type pos = line.find(";");
if ( pos != std::string::npos )
{
std::cout << line << std::endl;
}
instream.getline(get_next_line, 300);
}
cout << "There are " << num_lines << " lines of code in your program." << endl << endl ;
instream.close();
}
Dave Sinkula 2,398 long time no c Team Colleague
#include <iostream>
#include <string>
int main()
{
int lines = 0, lines_with_semicolon = 0;
std::string line;
while ( getline(std::cin, line) )
{
++lines;
std::string::size_type pos = line.find(";");
if ( pos != std::string::npos )
{
++lines_with_semicolon;
std::cout << line << std::endl;
}
}
std::cout << "lines = " << lines << std::endl;
std::cout << "lines_with_semicolon = " << lines_with_semicolon << std::endl;
return 0;
}
int lines = 0, lines_with_semicolon = 0;
std::string line;
++lines;
std::string::size_type pos = line.find(";");
++lines_with_semicolon;
std::cout << line << std::endl;
std::cout << "lines = " << lines << std::endl;
std::cout << "lines_with_semicolon = " << lines_with_semicolon << std::endl;
return 0;
lines = 24
lines_with_semicolon = 9
coolmel55 0 Light Poster
Not to be a pain now how do you read from an input file line by line
Dave Sinkula 2,398 long time no c Team Colleague
Open the file and read from it.
#include <iostream>
#include <fstream>
#include <string>
int main()
{
int lines = 0, lines_with_semicolon = 0;
std::string line;
std::ifstream file(__FILE__);
while ( getline(file, line) )
{
++lines;
std::string::size_type pos = line.find(";");
if ( pos != std::string::npos )
{
++lines_with_semicolon;
std::cout << line << std::endl;
}
}
std::cout << "lines = " << lines << std::endl;
std::cout << "lines_with_semicolon = " << lines_with_semicolon << std::endl;
return 0;
}
coolmel55 0 Light Poster
Never mind I did something stupid
THANKS a million!!!
coolmel55 0 Light Poster
Now I need to be able to count the total LOC in each object of the program as well as the number of methods in each object. I also need to get the name of the object.
HELP!!
coolmel55 0 Light Poster
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include "loc.h"
using namespace std;
//Constructor, initializes necessary variables
Lines::Lines()
{
}
//Requests a filename from the user
void Lines::count_lines()
{
char filename[16];
int lines = 0;
int lines_with_pound = 0;
string line;
cout << "Enter the name of the program that you would like data for: ";
cin >> filename;
ifstream instream(filename);
if(instream.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
while ( getline(instream, line) )
{
string::size_type pos = line.find("#");
if ( pos != string::npos )
{
++lines;
}
line.find("{");
if ( pos != string::npos )
{
++lines;
}
}
cout << "Total lines of code = " << lines << endl;
instream.close();
}
coolmel55 0 Light Poster
/* Contains implementation of all functions associated with the Lines Class */
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include "loc.h"
using namespace std;
//Constructor, initializes necessary variables
Lines::Lines()
{
}
//Requests a filename from the user
void Lines::count_lines()
{
char filename[16];
int lines = 0;
string line;
cout << "Enter the name of the program that you would like data for: ";
cin >> filename;
ifstream instream(filename);
if(instream.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
while ( getline(instream, line) )
{
string::size_type pos = line.find("#");
if ( pos != string::npos )
{
lines++;
}
if(string::size_type pos = line.find("}"))
{
if( pos != string::npos)
{
lines++;
}
}
if (string::size_type pos = line.find(";"))
{
if( pos != string::npos)
{
lines++;
}
}
}
cout << "\nTotal lines of code = " << lines << endl;
instream.close();
}
Stack Overflow 8 Junior Poster
Alright here we go. Simple changes, so lets go through them one step at a time:
> string::size_type pos = line.find("#");
This is fine, but lets change it around.
string::size_type pos;
pos = line.find("#");
This way we can use pos in other areas. Doing it the first way only lets us set pos there so if we look down at: line.find("{");
We see that we don't get the latest position.
Secondly, Use ++lines when the while loop starts. That way it counts the total lines. Next, lets check for '#':
pos = line.find("#");
if (pos != string::npos)
++lines_with_pound;
Here, we aren't incrementing lines but we are incrementing lines_with_pound. Do the same with the semicolon.
Here's a full look at this project:
void Lines::count_lines(void) {
char filename[16];
int lines = 0;
int lines_with_pound = 0;
int lines_with_semicolon = 0; // new
string line;
// Pos - Local variable
string::size_type pos;
cout << "Enter the name of the program that you would like data for: ";
cin >> filename;
ifstream instream(filename);
if (instream.fail()) {
cout << "Input file opening failed.\n";
exit(1);
}
while (getline(instream, line)) {
// Add to lines
++lines;
// If found, add here
pos = line.find("#");
if (pos != string::npos)
++lines_with_pound;
// If found add here
pos = line.find(";");
if (pos != string::npos)
++lines_with_semicolon;
}
// Display what we found
cout << "Total lines of code = " << lines << endl;
cout << "Total lines of code with pound = " << lines_with_pound << endl;
cout << "Total lines of code with semicolon = " << lines_with_semicolon << endl;
// Close file
instream.close();
}
Hope this helps,
- Stack Overflow
coolmel55 0 Light Poster
Now the trick is that I have to print out the object name and method counts for each object.
So I would have to use:
// If found, add here
pos = line.find("::");
if (pos != string::npos)
++lines_in_method;
//how would I stop this counting like from the { following the
//scope operator :: until the closing } and keep the method name?
Edited by happygeek because: fixed formatting
coolmel55 0 Light Poster
Now I need to be able to count the total LOC in each object of the program as well as the number of methods in each object. I also need to get the name of the object. Deadline fast approaching!
HELP!!
/* Contains implementation of all functions associated with the Lines Class */
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include "loc.h"
using namespace std;
//Constructor, initializes necessary variables
Lines::Lines()
{
}
//Requests a filename from the user
void Lines::count_lines()
{
char filename[16];
int lines_with_pound = 0;
int lines_with_semicolon = 0;
int num_of_methods = 0;
int lines = 0;
string line;
string::size_type pos;
cout << "Enter the name of the program that you would like data for: ";
cin >> filename;
ifstream instream(filename);
if(instream.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
while (getline(instream, line)) {
// Add to lines
++lines;
// If found, add here
pos = line.find("#");
if (pos != string::npos)
++lines_with_pound;
// If found add here
pos = line.find(";");
if (pos != string::npos)
++lines_with_semicolon;
// If found add here
pos = line.find("::");
if (pos != string::npos)
{
++num_of_methods;
}
}
// Display what we found
cout << "Total lines of code = " << lines << endl;
cout << "There are " << num_of_methods << " methods in your program!" << endl;
// Close file
instream.close();
}
coolmel55 0 Light Poster
/* Contains implementation of all functions associated with the Lines Class */
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include "loc.h"
using namespace std;
//Constructor, initializes necessary variables
Lines::Lines()
{
}
//Requests a filename from the user
void Lines::count_lines()
{
char filename[16];
int num_of_methods = 0;
int lines_in_method = 0;
int lines = 0;
string line;
string::size_type pos;
cout << "Enter the name of the program that you would like data for: ";
cin >> filename;
ifstream instream(filename);
if(instream.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
while (getline(instream, line))
{
// If found, add here
pos = line.find("#");
if (pos != string::npos)
++lines;
// If found add here
pos = line.find(";");
if (pos != string::npos)
++lines;
// If found add here
pos = line.find("::");
if (pos != string::npos)
{
pos = line.find("()");
if (pos != string::npos)
{
lines_in_method = 0;
++num_of_methods;
cout << "The method name is: " << line << endl;
// If found add here
pos = line.find(";");
if (pos != string::npos)
++lines_in_method;
// If found add here
pos = line.find("}");
if (pos != string::npos)
cout << << lines_in_method << << endl;
}
}
}
// Display what we found
cout << "Total lines of code = " << lines << endl;
cout << "There are " << num_of_methods << " methods in your program!" << endl;
// Close file
instream.close();
}
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.