Unable to open file linux

SFML — failed to load image — unable to open file — LINUX

I was developing my project on ubuntu (clion ide) but I changed distribution to xubuntu and cloned the same repository to it. Unfortunately, each image which I include in project generate this runtime error:

Failed to load image "image.png". Reason: Unable to open file 

SFML is installed correctly because I checked it with standard program from SFML Site. I didn’t have that problem on ubuntu.

My try

I think that there is a problem with permissions of files. So I create sample project (as below), added player.png and I get the same error. So I use that command in terminal:

It doesn’t solve a problem. I am new in linux so maybe there is something more to do, but I don’t know. I also tried different ways from this site and SFML forum but it doesn’t helps me.
I also tried to load other images, doesn’t work too.

Code

There is sample code which I have created to find problem. It is only modified sample from SFML Site.

#include int main() < sf::RenderWindow window(sf::VideoMode(400, 400), "SFML works!"); sf::Texture t; t.loadFromFile("player.png"); sf::Sprite sprite; sprite.setTexture(t); while (window.isOpen()) < sf::Event event; while (window.pollEvent(event)) < if (event.type == sf::Event::Closed) window.close(); >window.clear(); window.draw(sprite); window.display(); > return 0; > 

Solution and another problem:

solved the problem. By default, sfml expected the image to be in cmake-build-debug folder.
How to add default directory to my project, not to cmake folder?

Источник

Cannot open file with fopen(); function — C Programing [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.

This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.

  1. I am trying to create a program that using the fopen(); function.
  2. My problem is that fopen(); cannot find the file.
    • when i am using perror(«Error»); the output is Error: No such file or directory.
  3. I have read this articles and they did`not solved my problem:
    • fopen() returning a NULL pointer, but the file definitely exists
    • Unable to open a file with fopen()
    • It means i have tried to use fopen(); with the exec path to the file, and i have checked that the filename is the specific filename that i am looking for.
    • I have also used exec(«pwd»); to see that i am the correct directory.
  4. My OS is ubuntu mate 20.04.
  5. Here is my Code:
/* File Name: firstTransaction.c * File Mission: scan the input files and make the first transaction. */ #include "defines.h" /* * first transaction file: * Allocating new memory for the assembly source file. * Scanning the assembly source file. * @param filename - the filename of the file that needs to be converted by the program. * @return output and files - * output the assembly source file in a machine code. * return extern, entry and object files. */ /* function prototype */ extern char * readText(FILE * ptr, long ic, long dc); /* the function defined on assistanceFunctions.c file */ int firstTransaction(int * cf, char **av) < FILE * fp; /* a pointer for fopen function */ char * filteredFile = NULL; long ic = 100, dc = 0; /* declaration and initialization of the instruction counter and current data counter */ int i = 0, j = 0; /* indexes */ int cfh = 0; /* compatible file holder */ int fileNameLength = (int)strlen(av[i]); /* computing the first filename length to allocate memory */ char * fileName = (char*) calloc(fileNameLength,sizeof (char)); /* allocating memory for the first filename */ system("pwd"); while(cf[i] != 0) /* while there is more compatible files to open */ < cfh = cf[i]; /* cfh - compatible file holder, cf - compatible file array, set the next compatible file to cfh */ strcpy(fileName,av[cfh]); /* copy the file name from the argv array */ fp = fopen("fileName","r"); /* open the first compatible file for read */ if(fp == NULL) /* if file does not exists */ < perror("Error"); /* print the error - why the file not opened */ i++; /* increment i by one and try to open the next filename */ >else /* if the file was opened successfully, start scanning the file */ < /*filteredFile = */ readText(fp, ic, dc); /* calling readText function with pointer to the start of the file that was opend by fopen function */ /* while(filteredFile[j] != EOF) < putchar(filteredFile[j]); j++; >/* DONT FORGET TO CLOSE WITH FCLOSE(); */ > > return 0; > 

Here is some screenshots with the tested solutions that failed from the other articles:

Читайте также:  Консоль linux http запрос

triedSolutions

Edited after NeonFire answers:

  1. My first code used fp = fopen(fileName,»r»); but my error message was made manually this way: printf(«error, %s file does not exists\n», fileName); /* print error message to the user */
    • this cause that i did`not find my real error.
    • then i changed to fp = fopen(«fileName»,»r»); and used perror(«Error»); instead of the first and right way.
    • after i read NeonFire answers i got a new error Error: Too many open files.
    • i removed the backups file from the directory and i still have the same error.

Edited after Ted Lyngmo comment: i did had to close the file to solve the — «to many file to open error».

Источник

3 methods — How to fix «Vim can’t open file for writing error» | /etc/apt/sources.list E212: Can’t open file for writing

Vim can’t open file for writing error is one of the most common and frequent errors you will encounter, if you use Linux and vim editor. Whether you are a novice or Pro Linux user, «E212» is the error code, you don’t like seeing. Most of the time, this error occurs due to insufficient rights or a directory path or file that doesn’t exist, which you are trying to edit. In this post, I will let you know all the reasons and solutions to fix the «Vim can’t open file for writing» error.

When you use Linux, you can get the below-mentioned error, while you try to edit a file —

  • /etc/apt/sources.list» E212: Can’t open file for writing
  • Vim Can’t Save File (E212)
  • Vim can’t open file for writing
  • Root user can’t open file for writing
  • Error in editing vimrc
  • E212: cant open file for writing ec2
Читайте также:  Topaz gigapixel ai linux

Possible causes of the above-mentioned issues —

  • You don’t have access or you are trying to edit a file, which is owned by root user
  • Path of the file, you are trying to edit doesn’t exist (non existing file, non existing directory)
  • Trying to write a network drive and you don’t have access or partial access
  • Trying to edit a read only file system
  • Not enough space exists in your current partition or drive.

Let’s deep dive and fix errors and it’s causes one by one.

Install vim editor in Ubuntu, if not there already

If you get the error: «Command ‘vim’ not found, but can be installed with:» it means you don’t have vim (visual improved) editor installed. Run the following command to install the package in Ubuntu.

$ sudo apt install vim

Install vim in ubuntu using apt install vim command

How to Fix Vim can’t open file for writing error

Solution-1 Check the directory or path of file, create it if it doesn’t exist

First thing first, if you are getting vim can’t open file for writing error, check the path and directory of the file. Let’s understand it from an example.

vim can

I am trying to run a command «vim /home/tmp/sample.txt» and getting this error «/home/tmp/sample.txt» E212: can’t open file for writing» while saving it as shown in the image.

In this scenario, the «tmp» directory inside /home doesn’t exist. So if you will try to either create a new or edit the existing «sample.txt» file. This will not work and you will get errors.

In my case, I need to create the directory tmp inside the /home first to get rid of this "E21: can't open file for writing" error. This may happen in your case also. You may need to create the required directory or maybe you just need to correct the required path :-).
  • Press «ESC» on your keyboard.
  • Type «!sudo mkdir » path of directory that need to be created «. For example, in my case i need to create «tmp» directory inside /home.
:!sudo mkdir /home/tmp/

create directory using vim editor

validate directory after creating it from Vim editor

As soon as you will run this command and press enter, It may ask you to give your sudo credentials, and your required directory will get created as shown in the image.

You can edit, save and exit your file now with the «:wq» command without any issue.

How to get the reason of error «E21: can’t open file for writing» using vim help

To find out the reason for the error, follow these steps in the vim editor prompt, where you are getting an error.

how to get error help e212 in vim

  • Press «esc» key on your keyboard
  • Then type :h e212 as shown in image «:h -« represents help and e212 error. Same way you can check for any error, you encountered in vim editor
  • Press enter and you will get the error code and its reason. For example, in my case it is showing the follow —
For some reason the file you are writing to cannot be created or overwritten. The reason could be that you do not have permission to write in the directory or the file name is not valid. 

/etc/apt/sources.list E212: Can

Press the «ESC» key and then type «:q!» to exit from help.

How to fix etc/apt/sources.list» E212: Can’t open file for writing

If you use Linux, many times in your lifetime. You would have edited the sources.list file and I am sure, you would have encountered an E212 error. Follow solution-2 step by step to fix this issue.

Читайте также:  Создание загрузочного образа линукс

Solution-2 Validate User or file permissions

If you are trying to edit a file and you have given the correct path but still you are getting «can’t open file for writing error«, then you are a victim of permission issues.

If you are logged in with a non-root user, make sure you start with the sudo command, if you are trying to edit a system file or file owned by root.

Let me show you an example, try editing /etc/apt/sources.list with a non-root user and you will understand, what I am trying to prove here.

If you will see the below image, the «sources.list» file is owned by root, so if you will try to edit it using a non-root-user with the following command

$ vim /etc/apt/sources.list

check ther permission of file first, if you are not able to edit the file in vim

You will get «/etc/apt/sources.list» E212: Can’t open file for writing» error.

To fix it, you must edit this file using sudo permissions. run the following command to edit this file successfully.

$ sudo vim /etc/apt/sources.list

How to fix even if «Root user can’t open file for writing»

If you are a root or sudo user, Also you have given the correct path to directory or file but still, you are not able to open the file for writing. Then you are trying to edit a read-only file system or your disk space is full , which doesn’t allow editing and saving a file.

Solution3- Check for Read-only file system or Capacity issue

Use the «df» command to check the partition or disk space usage. If you see «disk» or «partition» available space is showing «0» bytes except for «/dev/loop».

Then make space by deleting or removing unnecessary files using the rm command. It will fix your «vim can’t open file for writing » issue.

check for capacity issue

If you have enough space, then make sure your file system is not mounted as read-only . Run the «mount» command and check for your device, it must be mounted as read-write (rw) not read-only (ro).

For example, /dev/sda5, which is my root (/) partition, mounted as rw.

If it is showing mounted as read-only in your case, then remount it as rw. If it’s happening due to file system errors, then fix the file system errors and re-mount it to a read-write file system .

$ sudo mount -o remount,rw /partition/identifier /mount/point or $ sudo mount -o remount,rw /

You will be able to edit your file, once your file system will be rw and your » vim can’t open file for writing » will vanish.

Video tutorial

Conclusion

I am hopeful, that one of the 3 solutions shared in this post will fix your «E212: Vim can’t open file for writing» error. If you are still facing issues, You can let me know, via your comments. I will try to help you to the best of my knowledge.

About The Author

Dev

Hi, My Name is Dev and as an Information Technology professional, My Life revolves around Gadgets, Servers, Storage, Software, Operating system, Internet and Cloud computing. My philosophy is “being healthy” and follow your “passion or hobbies” to make your life interesting. Please feel free to contact me for any questions, feedback and suggestions.

Источник

Оцените статью
Adblock
detector