『壹』 C语言如何将字符串写入文本文件
从键盘输入抄一行字符,写入一个文件, 再把该文件内容读出显示在屏幕上。
#include<stdio.h>
main()
{
FILE *fp;
char ch;
if((fp=fopen("string","wt+"))==NULL)
{
printf("Cannot open file strike any key exit!");
getch();
exit(1);
}
printf("input a string:\n");
ch=getchar();
while (ch!='\n')
{
fputc(ch,fp);
ch=getchar();
}
rewind(fp);
ch=fgetc(fp);
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(fp);
}
printf("\n");
fclose(fp);
}
『贰』 用C++字符串写入文件
示例:#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream fout;
// istream fin;
fout.open("c:\\1.txt");
fout<<"123"<<endl;
fout.close();
return 0;
}
『叁』 java中怎么将字符串按行写入到txt文件中
java写入文本文件的方法很多,比如FileWriter
FileWriterfw=newFileWriter("D:/Test.txt");
Strings="helloworld ";
fw.write(s,0,s.length());
s="helloworld2 ";
fw.write(s,0,s.length());
fw.flush();
这样就写了两行了。其中斜线n是换行符
『肆』 java怎么把大字符串写入文件
慢点应该没关系吧,可以考虑新建一个线程处理比较慢的业务,也就是说把写入字符串放到新建线程中执行,不影响主程序执行
『伍』 linux在shell下如何将字符串写入文件
可以使用echo命令将文本流导向标准输出,然后再使用>符号重新定向标准输出到文件。
用法内示例:容
将字符串"hello world"写入文件file.txt中
$ echo "hello world" > file.txt
然后再尝试用cat命令从读取文件并写入到标准输出,可以看到字符串已经成功写入文件。
$ cat file.txt
『陆』 如何将string字符串写入文档
有个类叫StringReader将字符串转为字符流
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
public class StringReaderTest {
public static final String SOURCE = "很长很长...";
public static void main(String[] args) throws IOException {
int length = 0; //每一次读取的长度
char[] buffer = new char[2048]; //设缓冲最大值为2048字符
//字符串转为字符流
BufferedReader br = new BufferedReader(new StringReader(SOURCE));
while((length = br.read(buffer)) != -1){ //若读到的不是末尾
System.out.println(new String(buffer, 0, length)); //做你想做的事
『柒』 java中如何把一段字符串写入文件中
使用Java中的File类,源url为文件的绝对地址,str为输入的字符串内容。
代码如下图所示:
String str="i love china!"
File txt=new File("url");
if(!txt.exists()){
txt.createNewFile();
}
byte bytes[]=new byte[512];
bytes=str.getBytes(); //新加的
int b=str.length(); //改
FileOutputStream fos=new FileOutputStream(txt);
fos.write(bytes,0,b);
fos.close();
『捌』 C语言怎么把字符串用fprintf写入文本文件
把把字符串写入文件,基本示例如下:
FILE *fp;
char s[]= "hello world!";
char c = ' ';
fp = fopen("file.txt","a");
fprintf(fp,"%s",s); //字符串使用%s
fprintf(fp,"%c",c); //字符使用%cfclose(fp);从文件读取到字符串 char s1[30];fp=fopen("file.txt","r");fscanf(fp, "%[^ ] ", s1);printf("%s ",s1);fclose(fp); 本来挺简单的一件事,可是让我头疼了好几个小时。
在前面写了 fp = fopen("file.txt","a");
fprintf(fp,"%s",s); //字符串使用%s
但是fclose(fp);这句被我写在了return 0;之前,然后字符串死活写不进文件里面去。后来终于发现是因为使用了while(1)循环读取端口数据,所以一直没有执行fclose(fp);这句,才导致文件里面一直是空的。所以fclose(fp);这句话不要忘记了哦~~ :)
『玖』 怎么将字符串写入文件
String s = "akjibuvuvu";
FileWriter fw = null;
File f = new File("a.txt");
try {
if(!f.exists()){
f.createNewFile();
}
fw = new FileWriter(f);
BufferedWriter out = new BufferedWriter(fw);
out.write(s, 0, s.length()-1);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("end");