I'm slowly plugging away at my music program. I would like to be able to pass a file name similar to this program below.
I'm trying to keep a text parsing program from ending if there are no new lines to parse and this is what came to mind. If there is a better solution I'd be grateful to learn.
This is working at the moment though with the exception of one issue.
when I pass file_name to the function check_file_size it transfers over fine.
I'm just not sure how to get the quotations marks around the passed file name so the ifstream myfile () can run it.
1 2 3 4
int check_file_size (std::string * a){
long start_of_file,end_of_file;
int size_arithmetic;
std::ifstream myfile (*a); //this line here is the problem
It should be std::ifstream myfile ("*a"); with quotations marks around it. but if i do that it thinks the entire thing is a string. hmmm.
// function to pause parsing if file size remains unchanged
// this would be called from another function and check if the file has changed
// size to resume parsing
#include <iostream>
#include <fstream>
int check_file_size(std::string * a);
int main () {
int file_size, file_size2;//variable find size of file
char exit_program; //variable to exit the program
std::string file_name = "testfile.txt";
std::string * p_file_name; //pointer for file_name
p_file_name = &file_name; //pointer equals address of file_name for passing between functions
file_size = check_file_size(p_file_name);
do {
file_size2 = check_file_size(p_file_name);
//putting this in for now so you can exit the program normally
std::cout << "Enter the letter 'x' to end program: ";
std::cin >> exit_program;
std::cout << "\n";
if (exit_program == 'x'){file_size2 = 0;} //set to 0 to end loop
} while (file_size == file_size2);
return 0;
}
int check_file_size (std::string * a){
long start_of_file,end_of_file;
int size_arithmetic;
std::ifstream myfile (*a); // This is the problem here
start_of_file = myfile.tellg();
myfile.seekg (0, std::ios::end);
end_of_file = myfile.tellg();
myfile.close();
std::cout << "size is: " << (end_of_file-start_of_file) << " bytes.\n";
size_arithmetic = (end_of_file-start_of_file);
return (size_arithmetic);
}
I'm also not sure if this is a great solution to pausing a parsing program either. Any advice would be appreciated.