标签: fclose

fclrite()在fclose()之后成功

我在fwrite()关闭流后遇到了一个奇怪的行为,fclose()但是文件没有被覆盖为fflush()失败.

我的代码是:

int main(int argc, char* argv[])
{
   FILE* file = fopen("file.txt", "w");
   if(!file) perror("Cannot open the file.\n");

    char text[] = "1234567";

    fclose(file);

    int count_of_written_objects = fwrite(text, sizeof(text),1, file);
    printf("Count of written objects into the file: %d \n", count_of_written_objects);

    if(count_of_written_objects != 1) perror("Not expected count of objects was written.\n");

    int success = fflush(file);
    printf("Variable success: %d \n", success);                                                                                                                
    if(success == EOF) perror("Flush did not succeed. \n");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它给出了以下输出:

Count of written objects …
Run Code Online (Sandbox Code Playgroud)

c fwrite fclose

1
推荐指数
1
解决办法
698
查看次数

程序崩溃与fclose

我正在使用以下文本文件:https://mega.nz/#!EN4iRJxA! VtKFEl9dlQHWHIzcgfOfpXTt9_ill_YkgsLWL3nORLg以下代码:

    #include <stdio.h>
    #include <stdlib.h>

    typedef struct
    {
        int id;
        char lastname[25];
        char name[20];
        float average;
    }student;

    int main(){
        int i;
        student *pointer= (student*) malloc(3);

        FILE *file= fopen("Struct.txt", "r");
        fread(pointer, sizeof(student), 3, file);
        for(i= 0; i< 3; i++){
            printf("Id: %d\nName: %s\nLast name: %s\nAverage: %.2f\n",
                (pointer+ i) -> id,
                (pointer+ i) -> name,
                (pointer+ i) -> lastname,
                (pointer+ i) -> average);
        }
        fclose(file);
        system("pause");
        return 0;   
    }
Run Code Online (Sandbox Code Playgroud)

当我删除fclose函数时,它可以工作,但我检查了它,似乎是正确的.我错过了什么?

c pointers fclose

1
推荐指数
1
解决办法
950
查看次数

C编程.使用fopen fclose进行文本文件操作

我打开一个文本文件并处理字数统计功能来计算单词和关闭文件.接下来,我再次打开相同的文件并将其存储在数组中,并限制数组中的字数值.

在这里,如果我在第1行和第16行使用fopen和fclose一次,我的程序不起作用.但是如果我打开它(第1行)处理它然后关闭它(第10行)并再次打开它(第12行)进行第二个处理,我的程序可以工作.这是否意味着fopen一次只能处理一个进程,我必须再次打开它进行第二个进程?

1. fptrr = fopen(fname,"r"); // open the text file in read mode
2. 
3.  if (fptrr == NULL) {//if file name does not match
4.       printf("Error: No such file or directory");
5.       return -1;
6.     }
7. 
8. wordCount += countWords(fptrr); //run word count function and get the value of total words
9. 
10. fclose(fptrr); // close the file
11. 
12. fptrr = fopen(fname,"r");
13. for(int i=0;i<wordCount;i++){ // define size of loop equal to words in a file …
Run Code Online (Sandbox Code Playgroud)

c fopen fclose

1
推荐指数
1
解决办法
362
查看次数

为什么fclose没有将文件指针设置为NULL?

我正在为FILE*编写RAII包装器.我注意到,当在析构函数中关闭后删除FILE*会导致未定义的行为(seg.错误或其他地方的错误).我假设fclose会将FILE*设置为NULL,但事实并非如此.

class smartFP {
  smartFP (const std::string& name) 
  : fp (fopen(name.c_str(), "r")
  { }

  ~smartFP()
  { 
     if (fp) {
        fclose(fp); 
        // delete(fp); <- This is causing crash
        fp = NULL; <- Is this OK?
     }
  }

private:
   FILE *fp;
};
Run Code Online (Sandbox Code Playgroud)
  • 为什么fclose没有将FILE*设置为NULL?
  • 第二个问题是fopen在堆或堆栈中为fp分配内存吗?我认为它在堆上做了,因此想在fclose之后进行删除,这样fp的堆上的4或8个字节就被解除分配了.但看起来这不是必需的.

c++ pointers fclose

0
推荐指数
1
解决办法
8828
查看次数

C - 将输出写入文件

EDIT: 

void print(const int *v, const int size) {
 FILE *fpIn;
 fpIn = fopen("char-array.txt", "a");
 int i;  
 if (v != 0) {
   for (i = 0; i < size; i++) {
     printf("%d", (int)v[i]);
     fprintf(fpIn, "%d\n", (int)v[i]);   
   }
   perm_count++;
   printf("\n");
 }
 fclose(fpIn);
} 
Run Code Online (Sandbox Code Playgroud)

我想这是一个相对简单的问题:)

基本上该程序使用置换算法,并将输出打印到控制台中的标准输出.我还想通过fprintf将内容写入文件.虽然我似乎无法让它工作.它只是将垃圾字符打印到文本文件的第一行,仅此而已!

我将粘贴下面的代码,非常感谢帮助!在print函数中可以找到写入文件代码.

谢谢,

T.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>

#include <time.h>
clock_t startm, stopm;
#define START if ( (startm = clock()) == -1) {printf("Error calling clock");exit(1);}
#define STOP if ( (stopm = …
Run Code Online (Sandbox Code Playgroud)

c io printf fopen fclose

0
推荐指数
1
解决办法
2万
查看次数

PHP将文件下载到服务器

我正在尝试使用来自php的curl下载图像,但是不是将图像保存到服务器,而是将随机字符输出到Web浏览器屏幕,这里是代码

$final['IMAGE'] = 'http://www.google.com/images/srpr/logo3w.png';

$imagename = trim(basename($final['IMAGE']));

$url  = $final['IMAGE'];
$path = '/media/import/';
$path .= $imagename;

$fp = fopen($path, 'w');

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);

$data = curl_exec($ch);

curl_close($ch);
fclose($fp);
Run Code Online (Sandbox Code Playgroud)

php fopen curl fclose

0
推荐指数
1
解决办法
5792
查看次数

主要的未定义方法'close':Ruby中的Object(NoMethodError)

filename = ARGV.first

txt = open filename

puts "Here's your file #{filename}:"
print txt.read

puts "Type the filename again: "
file_again = $stdin.gets.chomp

txt_again = open file_again

print txt_again.read

close(txt)
close(txt_again)
Run Code Online (Sandbox Code Playgroud)

程序运行正常,直到结束,但在打印第二个文件的内容后立即崩溃标题错误消息.

我使用(.class)检查了txt,txt_again并确认它们都是File对象.为什么不靠近工作?

ruby file fclose learn-ruby-the-hard-way

0
推荐指数
1
解决办法
638
查看次数

fgets只读取第一行文件

我试图从我的.cpp文件中读取一个文件.我正在使用C库,所以不要混淆.

所以问题就像我在标题中所说的那样清楚.fgets方法可以读取第一行,但是当它到达第二行时,它既不能读取第二行也不能读取文件的其余部分(因为它在出现问题时退出).

您可以找到相关的代码部分:

void read_input()
{
      int i = 0, N = 5;
  char str[STR_SIZE], line[STR_SIZE];
  FILE *fp;

  fp = fopen("out", "r");
  if (!fp)
    {
      fprintf(stderr, "error: file could not be opened\n");
      exit(1);
    }

  for (i = 0; i<2; i++)
    {
      if (fgets(str, STR_SIZE, fp) == NULL)
        {
          fprintf(stderr, "error: failed at file reading\n");
          exit(1);
        }

      if (feof(fp))
        {
          fprintf(stderr, "error: not enough lines in file\n");
          exit(1);
        }

      if ((sscanf(str, "%s", line) != 1) )
        {
          fprintf(stderr, "error: invalid file …
Run Code Online (Sandbox Code Playgroud)

c c++ fgets fclose

0
推荐指数
1
解决办法
1368
查看次数

如何使用FILE*fp调用函数关闭文件

在主要我创建一个文件指针,将其传递给一个函数来打开它,我想有一个单独的函数来关闭文件,但我无法关闭该文件.我错过了什么?

我没有收到错误,但我检查的方式表明文件没有关闭.

感谢您的帮助.

 int _tmain(int argc, _TCHAR* argv[])
{
    FILE *inFile, *outFile;
    FileOpen(&inFile, &outFile);
    FileClose(&inFile, &outFile);

    getch();
    return 0;
}

void FileOpen(FILE **inFile, FILE **outFile)
{
//--Open InFile--------------------------------
    if ((*inFile = fopen("a.txt","r")) == NULL){
        printf("\nError Opening File. ");
        exit(0);
    }
    else {
        printf("File Opened\n");
         }

//--Open OutFile--------------------------------
    if ((*outFile = fopen("b.txt","w")) == NULL){
        printf("\nError Opening File. ");
        exit(0);
    }
    else {
        printf("File Opened\n");
         }
}


void FileClose(FILE **inFile, FILE **outFile)
{
//--Close InFile----------
    if (fclose(*inFile)){
        printf("File Closed\n");
    }
    else { …
Run Code Online (Sandbox Code Playgroud)

c c++ function fclose file-pointer

0
推荐指数
1
解决办法
166
查看次数

即使在NULL检查后使用freopen,fclose(stdout)也会导致崩溃

我试图使用freopen打印stdout到文件,下面的代码崩溃.我是NULL检查,我很确定我在程序中的其他地方没有分段错误,因为我验证了我的所有malloc都是正确的.

程序到达时会崩溃fclose(stdout),并且不会打印我附加的错误信息来检查它.该printf("\nTestBeforeClose");行打印但printf("\nTestAfterClose");不打印.

int printAllVarRedir(char * filename, int redirmarker) {
    var *current = NULL;

    if (redirmarker == 1) {
        if (freopen(filename, "w", stdout) == NULL) {
            perror("Unable to open file");
            return 1;
        } else {
            if (head == NULL) {
                perror("No Variables");
                fclose(stdout);
                return 1;
            } else {
                current = head;
                printf("%s=%s. Address is %p.Next is:%p\n", current->varname, current->value, current, current->next);
                while (current->next != NULL) {
                    current = current->next;
                    printf("%s=%s. Address is %p.Next is:%p\n", current->varname, current->value, …
Run Code Online (Sandbox Code Playgroud)

c fclose freopen

0
推荐指数
1
解决办法
207
查看次数

fopen,fprintf,fclose的单线程?

我将一些非常临时的调试打印放到各种用户空间程序中,以确定哪些代码在嵌入式Linux设备上运行,我希望这些打印文件写入文件而不会让它打开.为了使调试在各种程序之间更容易移植,最好有一个可以打开文件,写入文件并关闭它而不必在别处定义函数/宏的单行程序.我可以这样做:

{ FILE *f = fopen("filename", "a"); if (f) { fprintf(f, "DEBUG MSG\n"); fclose(f); } else printf("File open error!\n"); }
Run Code Online (Sandbox Code Playgroud)

这只是删除空格:

{
    FILE *f = fopen("filename", "a");
    if (f) {
        fprintf(f, "DEBUG MSG\n");
        fclose(f);
    }
    else
        printf("File open error!\n");
}
Run Code Online (Sandbox Code Playgroud)

但这感觉不必要复杂.有没有更简单的方法来做到这一点?同样,我不是在谈论使它成为一个函数,因为我希望它可以在不同的程序之间进行复制/粘贴而不必每次都定义一个函数.它基本上是用户空间的临时printk等价物.

c linux printf fopen fclose

-4
推荐指数
1
解决办法
455
查看次数

标签 统计

fclose ×11

c ×8

fopen ×4

c++ ×3

pointers ×2

printf ×2

curl ×1

fgets ×1

file ×1

file-pointer ×1

freopen ×1

function ×1

fwrite ×1

io ×1

learn-ruby-the-hard-way ×1

linux ×1

php ×1

ruby ×1