如果有更有效的方法来执行以下操作:
DirectoryInfo di = new DirectoryInfo(@"c:\");
newFileName = Path.Combine(di.FullName, "MyFile.Txt");
Run Code Online (Sandbox Code Playgroud)
我意识到它只有两行代码,但考虑到我已经有了这个目录,感觉我应该能够做到这样的事情:
newFileName = di.Combine(“MyFile.txt”);
Run Code Online (Sandbox Code Playgroud)
编辑:
应该更清楚 - 我已经有了另一条道路,所以:
DirectoryInfo di = MyFuncReturnsDir();
newFileName = Path.Combine(di.FullName, "MyFile.Txt");
Run Code Online (Sandbox Code Playgroud) 我目前在这里有一个小程序,它会将.txt文件的内容重写为字符串.
但是我想将文件的所有内容收集为一个字符串,我该如何解决这个问题呢?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string file_name ;
while (1 == 1){
cout << "Input the directory or name of the file you would like to alter:" << endl;
cin >> file_name ;
ofstream myfile ( file_name.c_str() );
if (myfile.is_open())
{
myfile << "123abc";
myfile.close();
}
else cout << "Unable to open file" << endl;
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个包含需要提取并插入到新文本文件中的数据的大文本文件.我可能需要将这些数据存储在单元格/矩阵数组中?
但就目前而言,问题是我正在尝试测试较小的数据集,以检查下面的代码是否有效.
我有一个代码,在其中打开一个文本文件,扫描它并复制数据并将其保存在另一个名为"output.txt"的文本文件中.
问题:似乎没有正确保存文件.它只是在文本文件中显示一个空数组,例如this " [] ".原始文本文件只包含字符串.
%opens the text file and checks it line by line.
fid1 = fopen('sample.txt');
tline = fgetl(fid1);
while ischar(tline)
disp(tline);
tline = fgetl(fid1);
end
fclose(fid1);
% save the sample.txt file to a new text fie
fid = fopen('output.txt', 'w');
fprintf(fid, '%s %s\n', fid1);
fclose(fid);
% view the contents of the file
type exp.txt
Run Code Online (Sandbox Code Playgroud)
我从哪里去?
我想创建一个在Java中实现它自己的一些方法的接口(但是语言不允许这样做,如下所示):
//Java-style pseudo-code
public interface Square {
//Implement a method in the interface itself
public int getSize(){//this can't be done in Java; can it be done in C++?
//inherited by every class that implements getWidth()
//and getHeight()
return getWidth()*getHeight();
}
public int getHeight();
public int getWidth();
}
//again, this is Java-style psuedocode
public class Square1 implements Square{
//getSize should return this.getWidth()*this.getHeight(), as implemented below
public int getHeight(){
//method body goes here
}
public int getWidth{
//method body goes here
} …Run Code Online (Sandbox Code Playgroud) 我在iOS的My Documents文件夹中有一个文本文件,其中包含一个如下所示的链接:@"https://www.mysite.com"
我可以成功读取文件:
//READ TEXT FILE:
NSArray *paths5 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory5 = [paths5 objectAtIndex:0];
NSString *filePath5 = [documentsDirectory5 stringByAppendingPathComponent:@"mytext.txt"];
NSString *myContent = [NSString stringWithContentsOfFile:filePath5 encoding:NSUTF8StringEncoding error:NULL];
Run Code Online (Sandbox Code Playgroud)
NSString*myContent是实际链接:@"https://www.mysite.com
现在我通过NSURLRequest加载内容,如下所示:
NSURLRequest *request2 = [NSURLRequest requestWithURL:
[NSURL URLWithString:myContent]];
[[NSURLConnection alloc] initWithRequest:request2 delegate:self];
Run Code Online (Sandbox Code Playgroud)
我得到的问题/错误:
Connection failed: Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0xfe75cc0 {NSUnderlyingError=0xfe86ac0 "bad URL", NSLocalizedDescription=bad URL}
Run Code Online (Sandbox Code Playgroud)
但是,如果[NSURL URLWithString:myContent]]; 用字符串替换为[NSURL URLWithString:@"https://www.mysite.com"]],然后连接工作.
这里发生了什么事?为什么我不能只将文本文件中的url加载到我的URLWithString参数中?
我有一些简单的代码问题我根本无法解决.我正在使用Delphi 5并应用了所有更新.
我有这个代码块:
procedure TForm1.LoadBtnClick(Sender: TObject);
var
s1, s2, s3 : Textfile;
sa, sb, RString, SQLString : string;
begin
with sourcequery do begin
Close;
SQL.Clear;
SQL.Add('delete * from source');
ExecSQL;
Close;
AssignFile(S2, 'c:\delphi programs\events\source1.txt');
AssignFile(S1, 'c:\delphi programs\events\source2.txt');
Reset(s2);
Reset(s1);
while not eof(s1) do begin
readln(s1, RString);
SQLString := 'insert into source1(f1, f2) values(';
SQLstring := SQLString+ QuotedStr(copy(Rstring, 1, pos(chr(9), Rstring)-1))+', ';
SQLString := SQLString+QuotedStr(copy(Rstring, pos(chr(9),Rstring)+1,length(Rstring)))+')';
with sourcequery do begin
close;
SQL.Clear;
SQL.Add(SQLString);
ExecSQL;
end;
end;
end;
end;
Run Code Online (Sandbox Code Playgroud)
但是当我尝试编译它时,它会在"eof("之后停止:
while not …Run Code Online (Sandbox Code Playgroud) 正如您所知,这是一个类的编程任务.它已经过时了,我没有得到任何分数,但很快就会有测试,我想知道如何使用ADA中的特定功能.程序现在不同了,当我在初始程序GetStudent中使用test put语句运行它时,它会输出它们.但是现在它转到底部,第96行,并得到一个结束错误
with Ada.Text_IO;
use Ada.Text_IO;
with Ada.Integer_Text_IO;
USE Ada.Integer_Text_IO;
WITH Ada.Float_Text_IO;
USE Ada.Float_Text_IO;
procedure StudentFileLab is
------------------------------------------------------------------------
--| This program stores records in an array, reading them in from a file
--| and writing them out.
------------------------------------------------------------------------
subtype NameType is String(1..30);
subtype IDType is natural range 0..9999;
subtype GPAType is float range 0.0..4.0;
type StudentRecord is record
ID : IDType;
GPA : GPAType;
Name : NameType := (others => ' ');
end record;
subtype StudentIndex is integer range 1..100; …Run Code Online (Sandbox Code Playgroud) 我创建了一个Windows窗体应用程序,我有一个TextBox文本作为文件位置,如下所示:C:\DATA\FOLDER.
此外,我有一个文本文件,d:\data\test.txt其中包含许多行,但有一个定义的行Location="".
我如何复制行中文TextBox本文件test.txt中指定的位置Location="",而不提及行号?
我有一个每天生成的文本文件,并按以下格式命名:"year-month-date.txt"(例如2016-08-25.txt).
现在我想用python打开文本文件并从中读取一些数据.但我想这样做,而不是每次都手动更改代码中的文本文件的名称(记住每天更改文本文件的名称).
到目前为止我有(第6行是需要修复的部分):
#1 Get today's date
import datetime
todays_date=datetime.date.today()
print(todays_date)
#2 Import info from a text file that is named as today's date
filename=todays_date.txt
fin=open(filename,'r')
Line1list=fin.readline()
print(Line1list)
Run Code Online (Sandbox Code Playgroud) 我有一个我想读入的文本文件,并将文件中的每一行放入自己的字符串中.所以该文件将有4行:
2017-01-20
05:59:30
+353879833382
971575迈克尔
所以在代码中我需要在文件中读取并拆分每一行并将它们放入一个字符串中,即第一行将等于字符串日期,第二行将等于字符串时间等
码:
public static void ParseTXTFile(string FileName, int CompanyID)
{
try
{
FileInfo file = new FileInfo(FileName);
string Date;
string Time;
string Phone;
string JobNo;
string Name;
using (CsvReader reader = new CsvReader(new StreamReader(FileName), false))
{
while (reader.ReadNextRecord())
{
}
}
}
catch (Exception ex)
{
throw (ex);
}
}
Run Code Online (Sandbox Code Playgroud)
如何读取文件的每一行并将其设置为字符串?