我从这个站点得到了帮助,将对象的数据写入制表符分隔的文本文件.它很棒.问题是它在文件的末尾添加了一个额外的空行.因此,拾取文件的过程失败.如果有办法阻止代码加上额外的行,请告诉我.以下是代码:
private void WriteFile<T>(string filePath, IEnumerable<T> objectlist, string userName, string password)
{
bool createHeader = false;
if (!File.Exists(filePath))
{
using (File.Create(filePath)) ;
createHeader = true;
}
string data = ToCsv<T>("\t", objectlist, createHeader);
var file = new StreamWriter(filePath, true);
file.WriteLine(data);
file.Close();
}
Run Code Online (Sandbox Code Playgroud) 如何在c ++中读取空文件?
我用什么循环条件来读取空文件?
因为!fin.eof()条件不起作用并产生无限循环.
我使用turbo c ++,我有2个文件.音乐库文件已有一些专辑.我需要过滤掉并删除重复的相册并将其添加到filterfile中.
我的代码如下:
void albumfilter()
{
song s;
album a;
ifstream fin;
fstream finout;
fin.open("Musiclibrary.txt", ios::binary);
while(!fin.eof())
{
fin.read((char*)&s,sizeof(s));
if(fin.eof())
break;
finout.open("Filteralbum.txt", ios::binary| ios::in| ios::out);
while(!finout.eof())
{
finout.read((char*)&a, sizeof(a));
if(strcmp(a.getfilter_albumname(), s.getalbum())!=0)
{
strcpy(a.getfilter_albumname(),s.getalbum());
finout.write((char*)&a, sizeof(a));
finout.close();
}
}
}
fin.close();
}
Run Code Online (Sandbox Code Playgroud)
这段代码是否正确?
void maintainFileName ()
{
std :: ifstream myfile;
myfile.open ("zoomLevels.txt");
if (myfile.is_open ())
{
// Move to end of the file,
myfile.seekg (0, std::ios::end);
// and then six characters back to pick up the last file number.
myfile.seekg (6, std::ios::beg);
int len = 1;
char *t = new char[len];
myfile.read(t, len);
qDebug () << "\nt: " << *t << "\n";
}
else
{
qDebug () << "\nsorry";
}
}
Run Code Online (Sandbox Code Playgroud)
该文件包含:
78.8115,29.582,1,01.rda
78.8115,29.582,2,02.rda
76.3671,30.2201,1,11.rda
76.3671,30.2201,2,12.rda
78.1908,30.3007,1,01.rda
78.1908,30.3007,2,02.rda
77.3284,29.1415,1,01.rda
77.3284,29.1415,2,02.rda
77.3064,29.1655,1,01.rda
77.3064,29.1655,2,02.rda …Run Code Online (Sandbox Code Playgroud) 我在删除文本文件的内容时遇到了一些麻烦.据我所知,由于我们使用的PLM软件的权限问题,我似乎无法重命名或删除此文件并创建一个具有相同名称的新文件.不幸的是,我独自一人,因为似乎没有人知道到底出了什么问题.
但是,我可以读写这个文件.所以我一直在寻找seek命令并做这样的事情:
set f [open "C:/John/myFile.txt" "a+"]
seek $f 0
set fp [tell $f]
seek $f 0 end
set end [tell $f]
# Restore current file pointer
seek $f $fp
while { $fp < $end } {
puts -nonewline $f " "
incr fp
}
close $f
Run Code Online (Sandbox Code Playgroud)
这似乎用空格替换所有行,但我不确定这是解决这个问题的正确方法.有人可以给我一些指示吗?我还是比较新的Tcl.
谢谢!
当我创建我log.txt的File.Create(Path.Combine(PATH, NAME));并然后尝试从中读取时,我得到一个例外:{System.IO.IOException: The process cannot access the file 'c:\temp\log.txt' because it is being used by another process..
如果log.txt文件退出而不是在方法中创建,我可以读取和写入日志而不会出现任何问题.
是否log.txt创建了异步,问题是程序在创建之前是否尝试读取它?
public static void WriteToLog(string text)
{
try
{
if (!Directory.Exists(PATH))
{
Directory.CreateDirectory(PATH);
}
if( !File.Exists(Path.Combine(PATH, NAME)) )
{
File.Create(Path.Combine(PATH, NAME));
}
var logLines = File.ReadAllLines(Path.Combine(PATH, NAME)).ToList<string>();
logLines.Insert(0, "-------------------------------------------------End New Log");
logLines.Insert(0, text);
logLines.Insert(0, "-------------------------------------------------Start New Log");
File.WriteAllLines(Path.Combine(PATH, NAME), logLines);
}
catch (Exception ex)
{
}
}
Run Code Online (Sandbox Code Playgroud) 我试图使用java中的renameTo()将文件从一个目录移动到另一个目录,但是renameTo不起作用(不重命名和移动文件).基本上,我想首先使用相同的文件名删除文件,然后将文件从anoter目录复制到我最初删除文件的同一位置,然后复制具有相同名称的新文件.
//filePath = location of original file with file name appended. ex: C:\Dir\file.txt
//tempPath = Location of file that I want to replace it to file file without the file name. ex: C:\AnotherDir
int pos = filePath.indexOf("C:\\Dir\\file.txt");
//Parse out only the path, so just C:\\Dir
String newFilePath = filePath.substring(0,pos-1);
//I want to delete the original file
File deletefile = new File(newFilePath,"file.txt");
if (deletefile.exists()) {
success = deletefile.delete();
}
//There is file already exists in the directory, but I am just …Run Code Online (Sandbox Code Playgroud) 我在文件中写入一些文本,然后将其删除,但是删除失败。
代码很简单:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class TestFile {
public static void main(String[] args) throws IOException {
File file = new File("c:\\abc.txt");
writeFile(file, "hello");
// delete the file
boolean deleted = file.delete();
System.out.println("Deleted? " + deleted);
}
public static void writeFile(File file, String content) throws IOException {
OutputStream out = null;
try {
out = new FileOutputStream(file);
out.write(content.getBytes("UTF-8"));
} catch (IOException e) {
try {
out.close();
} catch (IOException e1) {
// ignored
}
} …Run Code Online (Sandbox Code Playgroud) 我在尝试将文件从服务器发送到客户端时遇到了一些麻烦.我似乎无法将相同的文件从服务器发送到客户端到两个SEPERATE文件.相反,它只是附加到第一个文件的末尾!任何帮助,将不胜感激.
编辑:我修改了代码.我已经将文件发送和接收任务模块化为2个函数'sendfile'和'recievefile'.我现在得到的错误是在第二次调用后套接字在sendfile和recievefile函数关闭.但我正在关闭的是文件,输出和输入流!也许关闭这些流也会关闭套接字......?无论如何,我试过不关闭输入和输出流,会发生什么 - 1)没有任何东西被转移到目标文件.所以我只是在服务器端获得一个空白的文件.2)甚至没有创建第二个文件.大.
像往常一样,任何帮助都会受到赞赏.
服务器:
package com.http.server;
import java.io.*;
import java.net.*;
public class Server
{
public static void main(String args[])throws Exception
{
System.out.println("Server running...");
/* Listen on port 5555 */
ServerSocket server = new ServerSocket(5555);
/* Accept the sk */
Socket sk = server.accept();
System.out.println("Server accepted client");
BufferedReader inReader = new BufferedReader(new InputStreamReader(sk.getInputStream()));
BufferedWriter outReader = new BufferedWriter(new OutputStreamWriter(sk.getOutputStream()));
/* Read the filename */
String serverlocation = "C:/Users/Arjun/Desktop/CNW model/HTTP Website/";
String filename = serverlocation + inReader.readLine();
if ( !filename.equals("") …Run Code Online (Sandbox Code Playgroud) 我有这个按钮点击事件:
private void button6_Click(object sender, EventArgs e)
{
if (File.Exists(@"d:\Keywords.txt"))
{
Dictionary<string,string> test = new Dictionary<string,string>();
string value_of_each_key;
string key_of_each_line;
string line;
int index;
sr = new StreamReader(@"d:\Keywords.txt");
while (null != (line = sr.ReadLine()))
{
index = line.IndexOf(",");
key_of_each_line = line.Substring(0, index);
value_of_each_key = line.Substring(index + 1);
test.Add(key_of_each_line, value_of_each_key);
}
sr.Close();
}
using (var w = new StreamWriter(@"D:\Keywords.txt"))
{
crawlLocaly1 = new CrawlLocaly();
crawlLocaly1.StartPosition = FormStartPosition.CenterParent;
DialogResult dr = crawlLocaly1.ShowDialog(this);
if (dr == DialogResult.OK)
{
if (LocalyKeyWords.ContainsKey(mainUrl))
{
LocalyKeyWords[mainUrl].Clear();
//probably …Run Code Online (Sandbox Code Playgroud) 可能重复:
在C中没有fclose的fopen
为什么我需要在C中使用fclose函数?我每次在c中打开它都会关闭文件,仍然只是为了知识我想知道如果我不使用fclose函数会发生什么?如果有人因为不使用fclose而遇到问题请分享.