Cannot read data from file

Hello,

I have developed an application that needs to read some data from file, but it seems to not work, here is my code:
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

 //this file exists, so no problem :/tmp/grab/applicationIconPath.txt
 std::ifstream applicationIconPath("/tmp/grab/applicationIconPath.txt");
 //i tried //tmp//grab//applicationIconPath.txt also but nothing changed

 std::string fileName;
 std::string fullPath;
 
 if(applicationIconPath.is_open())
 {
   
   std::getline(applicationIconPath,fileName);

   //the next line outputs nothing
   std::cout << fileName << std::endl;

   fullPath = "/usr/share/app-info/icons/archlinux-arch-extra/128x128/" + fileName;
   
   //the next line outputs /usr/share/app-info/icons/archlinux-arch-extra/128x128/ only
   std::cout << fullPath;
 }


 applicationIconPath.close();
 
 system("rm /tmp/grab/applicationIconPath.txt");
Last edited on
From your comments, it appears you are saying that it reaches lines 11 to 20, but outputs a blank line on line 15? Is this correct?

If so, it means you are successfully opening the file, but the getline call puts an empty string into fileName.

An additional check would be to wrap a check around the getline call:
1
2
3
4
if (!getline(applicationIconPath, fileName))
{
    std::cout << "Error reading applicationIconPath\n";
}


Note that you are calling rm on the file after you run it, so unless you are re-creating the file each time, you will get different behavior the second time you run your program.

Are you positive that the very first line of your file contains text?

Also, "/" is correct. "//" would not be. So you're good there.
Last edited on
Thanks for responding,

I made a mistake, when the image that i look for doesn't exist in :/usr/share/app-info/icons/archlinux-arch-extra/128x128/

The program writes nothing to that file, and thus it won't work in the code i posted, i am figuring out right now how to solve it.

Thank you and have a nice day.

mamograg.
if you want to open something only if it exists, there is a nocreate flag you can try. ios::nocreate I think.
Topic archived. No new replies allowed.