自从我接触C ++已经有好几年了,所以请忍受...
我的程序中发生内存泄漏,这会导致运行时错误。这会导致错误吗?
我有一个全局变量 FILE *fp;
在回调函数中,我有:
fp = fopen(filen,"w");
// do some writing
fclose(fp);
Run Code Online (Sandbox Code Playgroud)
使用相同的指针(fp),此过程重复多次。使用同一文件指针是否有问题?fclose()会自动为我释放内存,还是我需要手动删除它?如果我编写大量文本,是否存在任何可能导致运行时错误的限制?
谢谢!
在网页上,我使用以下代码将一些数据写入CSV文件,最后使用fclose()关闭;
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename='.$filename);
$out = fopen('php://output', 'w');
fputcsv($out, $cvs_cols);
fclose($out);
echo "HELLO WORLD"; // sneaks into CSV!?
Run Code Online (Sandbox Code Playgroud)
为什么已经有fclose()的“ HELLO WORLD”进入CSV下载文件?我想输出要在浏览器中显示的页面的其余HTML。我怎样才能做到这一点?
以下代码是否会导致未定义的行为?
FILE *fp;
fopen_s(&fp, "abc.bin", "rb");
fclose(fp);
fclose(fp); // accidentally closed an already closed file.
Run Code Online (Sandbox Code Playgroud)
我知道在已经释放的数组上调用free会导致UB.因此我问.
FILE *fp = fopen(filename, "r");
if (!fp)
{
fprintf(stderr, "Failed to open a file\n");
// fclose(fp) <-- should I close file here?
exit(1);
}
// Some operations on the file.
fclose(fp);
Run Code Online (Sandbox Code Playgroud)
如果fopen()无法打开文件,我还应该打电话fclose()吗?
我有一个关于文件指针的基本问题.在下面的代码中,我有一个while循环,后跟一个for循环.for循环只会显示行数,除非我再次打开文件 - 这是正常的吗?如果是的话,我应该FCLOSE它while循环事前后?可能有一些我不知道的"倒带"功能,所以我的整个方法可能都是错的.我意识到while循环和for可以组合,但我的问题是关于fopen,fclose和fopen再次使用数据.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAXLEN 200
enum { MAXLINES = 200 };
char lpath[MAXLINES][BUFSIZ];
int main(int argc, char **argv) {
char c;
int l = 0, n = 0, i = 0, count = 0;
char lines[MAXLINES][BUFSIZ];
FILE *fp = fopen(argv[1], "r");
if (fp == 0) {
fprintf(stderr, "failed to open input.txt\n");
exit(1);
}
while (l < MAXLINES && fgets(lines[l], sizeof(lines[0]), fp)) {
lines[l][strlen(lines[l])-1] …Run Code Online (Sandbox Code Playgroud) 我正在尝试"学习Ruby the Hard Way"并在尝试运行示例文件时收到未定义的方法'close'错误:http://ruby.learncodethehardway.org/book/ex17.html
我的代码,具体是:
from_file, to_file = ARGV
script = $0
puts "Copying from #{from_file} to #{to_file}."
input = File.open(from_file).read()
puts "The input file is #{input.length} bytes long."
puts "Does the output file exist? #{File.exists? to_file}"
puts "Ready, hit RETURN to contine, CTRL-C to abort."
STDIN.gets
output = File.open(to_file, 'w')
output.write(input)
puts "Alright, all done."
output.close()
input.close()
Run Code Online (Sandbox Code Playgroud)
我收到的错误仅适用于最后一行'input.close()',因为'output.close()'似乎工作正常.作为参考,我正在使用预先存在的输入文件,并创建一个新的输出文件.
提前致谢.
从我读到的内容来看,fclose()基本上就像分配了内存时的free(),但我还读到操作系统会为你关闭该文件并冲掉它终止后打开的任何流.我甚至测试了一些没有fclose()的程序,它们似乎都运行良好.
这是程序:
#include <stdio.h>
#define BUF_LEN 200
#define LINE_NUMBER 3
int line_len(char* filename, int n)
{
FILE* f;
char buf[BUF_LEN];
int j, i = 0;
if ((f = fopen(filename, "r")))
{
for (j = 0; j < n; j++)
fgets(buf, BUF_LEN, f);
for (i = 0; buf[i]; i++) /* find end of buf */ ;
fclose(f);
}
return i;
}
int main()
{
printf("%d\n", line_len("test.txt", LINE_NUMBER));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
根据我的理解,函数line_len接收文件的名称和我们感兴趣的行数.然后它以只读模式打开文件并迭代直到到达行n,通过每次迭代BUF_LEN-1从文件中读取字符f并存储这些人物buf.因此当第一个for循环中断时,buf …
假设我想将passwd内容存储在一个变量中,就像这样:
local passwd = io.open('/etc/passwd', 'r'):read('a')
Run Code Online (Sandbox Code Playgroud)
我读完后没有关闭文件可以吗?我应该像这样重写它:
local f = io.open('/etc/passwd', 'r')
local passwd = f:read('a')
f:close()
Run Code Online (Sandbox Code Playgroud)
我知道第一段代码有效,但我没有——如果它会导致一些隐藏的问题。
我正在使用 Lua 5.3
我在网上没有找到答案,如果有的话请链接...
#include <stdio.h>
int main(){
FILE*fp;
char arr[4];
printf("Opening...\n");
fp=fopen("slide.txt", "r");
printf("Opened\n");
if(fp==NULL){
printf("No such file\n");
return 1;
}
fscanf(fp, "%s", arr);
printf("Printing...\n");
printf("%s\n", arr);
printf("Printed\n");
printf("Closing...\n");
fclose(fp);
printf("Closed\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
文件内容:
ciao
Run Code Online (Sandbox Code Playgroud)
我的输出:
Opening...
Opened
Printing...
ciao
Printed
Closing...
Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)