Как проверить что файл пустой c
Перейти к содержимому

Как проверить что файл пустой c

  • автор:

как проверить пустой ли файл.

пустой, т.е. не содержит букв и цифр, но может содержать пробелы.

файлы doc, txt. может есть какая-то функция?

Проверить, пустой ли файл и записать в него текст
Есть N-количество папок, в етих папках есть файл out.txt, нужно чтоб файл out.txt, проверялся, если.

Как записать в бинарный файл пустой байт
Приветствую вас. Вот какая проблема, есть бинарный файл его структура такова, 55 байт первые 4.

Как проверить пустой ли входной файл?
Если входной файл пустой, то программа должна сообщить об этом. Что нужно подставить в if для.

Как проверить пустой файл или нет?
Всем доброго времени суток, столкнулся с такой проблемой: нужно считать из файла данные и записать.

Проверка на пустой файл в C ++

Есть ли простой способ проверить, является ли файл пустым. Например, если вы передаете файл функции, и вы понимаете его пустым, то сразу же закрываете его? Спасибо.

Изменить, я пробовал использовать метод fseek, но я получаю сообщение об ошибке «Не могу преобразовать ifstream в FILE *».

Мой параметр функции

7 ответов

Возможно, что-то похожее на:

Короткий и сладкий.

С учетом вашей ошибки другие ответы используют доступ к файлам в стиле C, где вы получаете FILE* с определенными функциями.

Напротив, мы с вами работаем с потоками С++ и поэтому не можем использовать эти функции. Вышеприведенный код работает простым способом: peek() заглянет в поток и вернет, не удаляя, следующий символ. Если он достигнет конца файла, он возвращает eof() . Ergo, мы просто peek() в потоке и видим, если он eof() , так как пустому файлу нечего заглядывать.

Примечание. Это также возвращает true, если файл никогда не открывается в первую очередь, что должно работать в вашем случае. Если вы этого не хотите:

How to check in C if the file is empty?


Bridge WordPress Theme

Salient WordPress Theme

I’m working with C, and it turns out that I have a function or method that you have to print the data, the meal markings, for students who book a meal on the system.

In the method the system must check if this file is empty and if it is empty it simply prints to the file fprintf(filetmp, «%d %s %s %s \n», num, nome, subnome, meal);

If the file already has, for example, two meals marked by the students, the system must read and print them to filetmp

I have tried to compare the file to see if it is empty, or if you have a meal, I checked and consulted the internet for it and although it exists, I know a ftell(file) function that allows determining how file is empty or no I can not in any way test whether the file has a meal or not.

I also tried feof(file) but it happens that I just run out of the file. How can I change the code for it to check if the file is empty, and to check if the file has any meals.

2 answers

You can check the size of the file and then check if it is empty.

Example, verifying that the teste.txt file is empty:

You can do a basic replay system to check if there is something in the file

check if file is empty

Member Avatar for cyberguy007

Is there any if statement to check if a file is empty or not?

  • 6 Contributors
  • 11 Replies
  • 27K Views
  • 1 Year Discussion Span
  • Latest Post 11 Years Ago Latest Post by sanof3322

Recommended Answers Collapse Answers

Assuming by «empty» you mean if you try to read any characters, the input will fail immediately at end-of-file, then testing for an empty file is trivial:

Assuming same things as Narue, you can also just peek the character
without reading it, just in case the file is not empty.

All 11 Replies

seek to end of file, then get the file pointer. If 0, its an empty file. How to do that depends on whether you want to use fstream or FILE.

Member Avatar for cyberguy007

Wel i used fstream.

Assuming by «empty» you mean if you try to read any characters, the input will fail immediately at end-of-file, then testing for an empty file is trivial:

Member Avatar for mrnutty

Assuming same things as Narue, you can also just peek the character
without reading it, just in case the file is not empty.

We’re a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.

Reach out to all the awesome people in our software development community by starting your own topic. We equally welcome both specific questions as well as open-ended discussions.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *