我有一个具有通道结构的程序:
struct channel
{
char title[40];
float gain;
float offset;
};
int main (int argc, char **argv)
{
struct channel channels[8];
}
Run Code Online (Sandbox Code Playgroud)
并且调用的文本文件configurationSettings.txt包含填充8个通道所需的信息:
Title1
20
30
Title2
10
0
Title3
34
03
...
Run Code Online (Sandbox Code Playgroud)
我如何将数据从文本文件中提取到适当的变量中?
编辑:
这是我到目前为止的方向:
FILE *fptr;
if ((fptr=fopen("configurationSettings.txt","r"))==NULL){
printf("\n\nConfiguration file not found.\n");
// exit(1); // Program exits if file pointer returns NULL.
}
while (1) {
if (fgets(loadedTitle,150, fptr) == NULL) break;
if (fgets(loadedGain,150, fptr) == NULL) break;
if (fgets(loadedOffset,150, fptr) == NULL) break;
printf("%s", loadedTitle); …Run Code Online (Sandbox Code Playgroud) 我有一个文本文件,其中包含我的计算机上保存的内容abcdefgh.我想使用FileInputStream来显示控制台上的字符,同时还要测量执行此操作所需的时间.它看起来像这样:
public class Readtime {
public static void main(String args[]) throws Exception{
FileInputStream in=new FileInputStream("bolbol.txt");
while(in.read()!=-1){
long startTime = System.nanoTime();
int x = in.read();
long endtime = System.nanoTime();
System.out.println(endtime-startTime);
System.out.println((char)x);
}
in.close();
}
}
Run Code Online (Sandbox Code Playgroud)
我在控制台上得到的是以下内容:
8863
b
7464
d
6998
f
6997
h
Run Code Online (Sandbox Code Playgroud)
其余的字母现在在哪里?就好像只进行了4次读操作一样.我的想法是朝着字符大小的方向前进,一次read()只能读取一个字节,但我没有到达任何地方.
我想String使用分隔符","在文件中提取第一个.为什么此代码生成大于一行的行数?
public static void main(String[] args) {
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader("irisAfter.txt"));
String read = null;
while ((read = in.readLine()) != null) {
read = in.readLine();
String[] splited = read.split(",");
for (int i =0; i<splited.length;i++) {
System.out.println(splited[0]);
}
}
} catch (IOException e) {
System.out.println("There was a problem: " + e);
e.printStackTrace();
} finally {
try {
in.close();
} catch (Exception e) { e.printStackTrace(); }
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试学习如何使用文本文件中的逐行读取.即使我将txt文件放在同一个src中,控制台也始终将错误显示为 - No such file or directory.
public class ddd {
public static void main(String[] args) {
FileInputStream fis = null;
BufferedReader reader = null;
try {
fis = new FileInputStream("/dd/src/com/dd/input.txt");
reader = new BufferedReader(new InputStreamReader(fis));
System.out
.println("Reading File line by line using BufferedReader");
String line = reader.readLine();
while (line != null) {
System.out.println(line);
line = reader.readLine();
}
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
reader.close();
fis.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
} …Run Code Online (Sandbox Code Playgroud) 也许这是一个非常愚蠢的问题(我非常喜欢写/读/操作文件)但我尝试了不同的方法(在网上和网站上找到)但没有任何效果:/我的矩阵是用文件写的,在矩阵的每个元素之间,有一个","像分隔符.我想在每个元素之间","而不是在行的末尾,所以我再次打开文件并尝试删除每行的最后一个字符,但我的代码不起作用!
//writing on the file
using (TextWriter tw = new StreamWriter("PATH"))
{
for (int i = 0; i < colonne; i++)
{
for (int y = 0; y < righe; y++)
{
tw.Write(matrice[y, i]+",");
}
tw.WriteLine();
}
}
using (FileStream fs = File.OpenWrite("PATH"))
{
fs.SetLength(fs.Length - 1);
fs.Close();
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
最近我和我的朋友们对不同的编程语言进行了基准测试 当我最近学习Haskell时,我想表明一个函数式语言的表现几乎和C一样好,代码更简单.但下面粘贴的代码,用GHC的-O3选项编译,在我的机器上执行大约1.6秒.Python和Ruby中的等效脚本执行得更快(这是一个简单的for循环).
import System.IO
saveLine fh x = hPutStrLn fh $ show x ++ "\t" ++ show (x^2)
main = do
fh <- openFile "haskell.txt" WriteMode
mapM (saveLine fh) [1..999999]
hClose fh
Run Code Online (Sandbox Code Playgroud)
你可以在这里查看其他语言的代码(我只写了Python和Ruby中的代码).
问题是 - 如何让它运行得更快?
我有一个文本文件中的数据,如下所示:
2,20 12,40 13,100 14,300
15,440 16,10 24,50 25,350
26,2322 27,3323 28,9999 29,2152
30,2622 31,50
Run Code Online (Sandbox Code Playgroud)
我想在Python中将这些数据读入两个不同的列表.但是,这不是CSV文件.数据读取如下:
mass1,intensity1 mass2,intensity2 mass3,intensity3...
我应该如何将群众和强度读入两个不同的名单?我试图避免编写此文件以使数据更整洁和/或以CSV格式.
我看起来非常广泛,但我无法找到有关\bJava中字符范围的信息.
注意:我正在使用Eclipse,并且知道它仅限于不支持该字符.(如果你想回答的问题:寻找支持\bchar 的控制台eclipse插件)
我想知道两件事:
我可以将退格字符写入文件以删除以前编写的测试吗?(我不需要这样做,但如果我不能用于我的程序,它会降低兼容性)
我可以删除换行符(
\r\n或其中一个)\b吗?
如何将listing/0SWI-Prolog REPL 的输出写入文件?
?- listing > file.txt.
Run Code Online (Sandbox Code Playgroud) 我正在尝试打印文件'words'中的最长行,这是一个带有单词列表的文件(每个都在一个新行中).使用下面的代码,我可以打印出比之前更长的每一行.但是,我需要它才能打印出整个文件中最长的一行.
我仍然是Ruby的新手,我似乎无法在谷歌上找到答案.
max = 0
IO.foreach('words') do |line|
if line.length > max
max = line.length
print line
end
end
Run Code Online (Sandbox Code Playgroud)
我将衷心感谢您的帮助.