我希望我的程序打开一个文件,如果它存在,或者创建该文件.我正在尝试以下代码,但我在freopen.c上得到一个调试断言.我会不会更好地使用fclose然后立即fopen?
FILE *fptr;
fptr = fopen("scores.dat", "rb+");
if(fptr == NULL) //if file does not exist, create it
{
freopen("scores.dat", "wb", fptr);
}
Run Code Online (Sandbox Code Playgroud) 我试图使用freopen()打印到文本文件和屏幕,但我只是实现打印到文件.
我想知道是否有一个容易将程序输出保存到文件并将其打印到屏幕?因为我以另一种方式工作,但最终我不得不打印两次声明.一个用于文件,另一个用于输出.
注意:我是C++的新手,我正在尝试为下一学期的课程学习它,所以需要直接的答案,因为我已经在线查看,除此之外找不到任何简单的答案.
这是我到目前为止:
#include<iostream>
#include<time.h>
#include<stdlib.h>
#include<fstream>
using namespace std;
void menu(){
cout << "\t********************************************************\n"
<< "\t* Welcome to slot machine. *\n"
<< "\t* Would you like to play? (1 to play, 2 not to play) *\n"
<< "\t********************************************************\n\n";
return;
}
void update(int arr[], int &token) {
if (arr[0]==arr[1] && arr[1]==arr[2]) {
token+=4;
cout << "You win\n\n";
} else if (arr[0]==arr[1] || arr[1]==arr[2] || arr[0]==arr[2]) {
token+=1;
cout << "You got two out of three\n\n";
} else {
token-=1; …Run Code Online (Sandbox Code Playgroud) 我正在使用:
#if TARGET_IPHONE_SIMULATOR == 0
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"console.log"];
freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
#endif
Run Code Online (Sandbox Code Playgroud)
..将NSLog重定向到一个文件,这个文件很有用.
我想记录文件,我的应用程序的用户可以打开和关闭..所以有谁知道如何将NSLog/stderr重定向回控制台?
谢谢!
请提出一种在Java中获得类似行为的方法,就像我们在C中执行freopen(“ filename”,“ r”,stdin)或freopen(“ filename”,“ w”,stdout)一样。
以下计划:
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
int main() {
fclose( stderr );
printf( "%d\n", fileno( stderr ) );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
显示-1在Ubuntu 11.04和2对ICS 4.0.3模拟器.找不到有关此问题的任何信息 - 我可以在两个平台上使此代码的工作类似吗?freopen上stderr有同样的问题.
更新:
以前的小程序演示了我遇到的实际问题的原因:如果我尝试freopen stderr在不存在的目录中存档,在linux stderr上关闭但在android上它保持打开状态!甚至更多 - 如果我在这个打开的stderr文件中写smth 然后fopen在其他文件stderr上写,我打印的文本将写入此打开的文件.
那么,这个程序:
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
# define LOGD( ... ) printf( __VA_ARGS__ ); printf( "\n" )
# ifdef ANDROID
# define HOMEDIR "/data/data/com.myapp/" …Run Code Online (Sandbox Code Playgroud) 在编写c/c++代码时,使用freopen()非常方便。请参阅以下代码片段 -
int main(){
int n1, n2, result;
freopen("input.txt", "rb", stdin);
freopen("output.txt", "wb", sdtout);
while(scanf("%d %d", &n1, &n2)==2 && n1>0 &&n2>0){
...
...
...
printf("%d\n", result);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
The use of freopen() in this way is very useful when we are trying to debug/test a small console application. We can put the sample input in 'input.txt' file once and reuse them every time instead of giving the input manually in terminal/console. And similarly we …
我发现这个问题适用于Python,Java,Linux脚本,但不是C++:
我想将C++程序的所有输出写入终端和输出文件.使用这样的东西:
int main ()
{
freopen ("myfile.txt","w",stdout);
cout<< "Let's try this";
fclose (stdout);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
将其输出到名为"myfile.txt"的输出文件,并阻止它显示在终端上.如何同时输出?我使用visual studio 2010 express(如果这会有所不同).
提前致谢!
我正在编写一段代码,该代码获取一个文件并将其保存到另一个具有不同名称的文件中 - 但是,我不确定是否需要关闭这两个文件?
FILE *logfile = fopen("log.txt", "a+");
while(1) {
char filename[500];
char logline[512];
char channel[512];
//Do stuff
sprintf(filename, "%s.log.txt", channel);
freopen(filename, "a+", logfile);
log_to_file(logline, logfile);
}
Run Code Online (Sandbox Code Playgroud) 在CI中可以使用scanf和printf通过管道来读取和写入文件,如下所示:
freopen ("input.txt", "r", stdin);
freopen ("output.txt", "w", stdout);
Run Code Online (Sandbox Code Playgroud)
在Java中,您也可以这样做
System.setIn
Run Code Online (Sandbox Code Playgroud)
和朋友.
如果您需要经常使用文件和stdin/stdout进行交换,以及是否保持代码不受文件指针的影响,这非常方便.
Go有类似的东西吗?
我试图从vs17中的文件中读取.但这里系统("暂停")不起作用.这里的控制台窗口弹出并消失.input.txt文件只包含一个整数.
#include<iostream>
#include<stdio.h>
#include<cstdio>
#pragma warning(disable:4996)
using namespace std;
int main()
{
freopen("input.txt", "r", stdin);
int n;
cin >> n;
cout << n << endl;
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
那么有没有办法从文件中读取并在控制台中显示输出,直到给出键盘的另一个输入.提前致谢