Passing file name to ifstream myfile

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.


Full program here.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// 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.
Last edited on
Why do you NEED a std::string pointer? Just pass your 'file_name' string to check_file_size() as a reference, and then in check_file_size():

1
2
3
4
int check_file_size(const std::string& string) {
//blah...
std::ifstream myfile(string.c_str());
//... 
Last edited on
A string pointer is all I knew about at the time.

That c_str() function does seem useful though. I might be able to use that to add on apostrophes to the beginning and end of the file_name string.

I'll have to read about how to deal with apostrophes. The compiler seems to have a hard time dealing with them as part of a string.
See "escape codes" under Character and string literals here:
http://www.cplusplus.com/doc/tutorial/constants/
Thank you both. combining your two suggestions fixed my problem :)
Topic archived. No new replies allowed.