Results 1 to 4 of 4

Thread: Working with files - use in different functions?

  1. #1
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Working with files - use in different functions?

    Hi all I'm not too good at working with files, they confuse the hell out of me. However i have this class, let call it test.cpp

    Qt Code:
    1. #include <iostream>
    2. #include <string>
    3. #include <fstream>
    4. #include <QtGui>
    5.  
    6. using namespace std;
    7.  
    8.  
    9. class test {
    10.  
    11. private:
    12.  
    13.  
    14. public:
    15. ifstream myfile;
    16. bool open_file();
    17. void close_file();
    18. void next_question();
    19. };
    20.  
    21. bool open_file(){
    22.  
    23. myfile.open("C:\\test.txt");
    24. if(myfile.is_open()){
    25. qDebug() << "Open";
    26. return true;
    27. }
    28. else
    29. {
    30. qDebug() << "Error - file not opened";
    31. return false;
    32. }
    33. }
    34.  
    35. void close_file(){
    36.  
    37. myfile.close;
    38.  
    39. }
    40.  
    41.  
    42. void next_line(){
    43.  
    44. if(open_file()){
    45.  
    46. // Code here to do stuff with myfile
    47.  
    48. }
    49. else
    50. {
    51. qDebug() << "File not opened";
    52. }
    53.  
    54.  
    55. }
    To copy to clipboard, switch view to plain text mode 


    The error i keep getting is:

    " 'myfile' was not declared in this scope. "

    How do i work with myfile across all of my functions within a class?


    Thanks for your time and trouble

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Working with files - use in different functions?

    You might want to read about basic C++ stuff such as scopes. It has to be something like
    Qt Code:
    1. bool test::open_file(){
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Working with files - use in different functions?

    And since you are using Qt you can use QFile

  4. #4
    Join Date
    Oct 2010
    Posts
    54
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Working with files - use in different functions?

    Thanks for all those who replied. I have got it working now and feel like I have a much better understanding now

    Thanks

Similar Threads

  1. Advise sought on working with binary files across OSes
    By TorAn in forum General Programming
    Replies: 2
    Last Post: 19th September 2010, 06:44
  2. Auto-discovery of include files is not working
    By Forte Systems in forum Newbie
    Replies: 2
    Last Post: 3rd June 2010, 16:11
  3. Replies: 0
    Last Post: 16th April 2010, 11:16
  4. aux functions not working on opengl
    By john_god in forum Qt Programming
    Replies: 7
    Last Post: 11th April 2009, 12:23
  5. Resources files (.qrc) not working
    By degs2k4 in forum Qt Programming
    Replies: 16
    Last Post: 21st February 2008, 09:54

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.