我需要读取ML(SLMNJ)中的文件并将其保存在某些结构中.我需要阅读一些指向图形声明的数据:
[( 1 , 2 , 13 ),( 2 , 3 , 3 ),( 2 , 4 , 8 ),( 2 , 5 , 4 ),( 3 , 1 , 5 ),( 3 , 4 , 1 ),( 4 , 6 , 5 ),( 5 , 5 , 5 ),( 6 , 4 , 6 )]
Run Code Online (Sandbox Code Playgroud)
(第一个数字:节点的名称,第二个数字:连接节点的名称,此鬃毛的第三个数字权重(每个()显示一个鬃毛))
对于expamle,这是测试输入如何读取文件以及保存它的结构
嗨,我是Perl编程的新手.我正在尝试读取一个.csv由逗号分隔的2个字段的文件.
我想以密钥及其值的形式将文件的所有数据放入哈希.
我的输入文件是
2.8, gitu 2.5, Has 2.7 Hwait 3.1-weiity 4.2, city 2.7:query 4.9, city 16.2, play 6.2, game 7,,,8 Jami 4.0, city
此行代码不会打印输入文件中存在的所有有效数据.有效数据采用行开头的形式,数字之间有逗号,然后是名称.否则应忽略其他无效条目.当我打印时,它会跳过几个有效的条目显示%hashforHighMagnitude;
请告诉我我在哪里丢失?如何获得所有有效条目%hashforHighMagnitude;
我想这样做
open ( OF, "$inputFile") or die "Cant open input file: $!\n";
while ( $Line =<OF>) {
if($Line =~ /^\d+\.+\d*\s*,\s*\w+$/g)
{
( my $magnitude, my $place ) = split(/,/,$Line);
$hashforHighMagnitude{$place} = $magnitude;
$hash{$place}++;
}
else
{
next;
}
}
print %hashforHighMagnitude;
close(OF);
Run Code Online (Sandbox Code Playgroud)
输出应该是
2.8, gitu 2.5, Has 4.2, city 4.9, …
我正在尝试在R中导入一个csv文件.我已经完成了几次但是使用这个特定的文件它会返回一个错误.
在csv的第一行,我有部分文本和部分编号的列的名称,因为它们代表月,年和一些观察点的数量..csv文件,即使更大,看起来如下:
Mo,Yr2,4,10,32,38,41,60,63,82
9,1980, 6.0, 0.2, 0.7, 1.0, 0.4, 0.7, 0.4, 1.5
10,1980, 25.1, 39.7, 41.4, 15.5, 20.8, 43.6, 37.1, 17.8
11,1980, 11.5, 8.6, 23.6, 7.5, 15.6, 12.2, 13.4, 7.6
12,1980, 59.6, 90.0, 103.9, 50.0, 67.1, 109.2, 81.6, 48.4
Run Code Online (Sandbox Code Playgroud)
我尝试了以下错误:
> m <- read.csv(file="my_file.csv", sep=",",head=TRUE)
Error in read.table(file = "my_file.csv", sep = ",", head = TRUE) :
duplicate 'row.names' are not allowed
Run Code Online (Sandbox Code Playgroud)
所以我试过了:
> m <- read.csv(file="my_file.csv", sep=",",head=TRUE,row.names=NULL)
> m
row.names Mo Yr2 X4 X10 X32 X38 X41 X60 X63 …Run Code Online (Sandbox Code Playgroud) 我对Haskell的IO有点新意,尽管我经常阅读它,但我的代码仍然无效.
我想要该应用程序做什么:
这是我到目前为止的代码.我可以确保函数"middle"在传递[String]时工作正常.
middle :: [a] -> a
middle xs = (drop ((l - 1) `div ` 2) xs) !! 0
where l = length xs
getSortedMiddleElement :: Int -> String
getSortedMiddleElement i = do
dat <- readFile $ "file" ++ (show i) ++ ".txt"
return $ middle $ sort $ lines dat
Run Code Online (Sandbox Code Playgroud)
我从"Int - > Content"函数(我使用Yesod)调用getSortedMiddleElement,其中数字通过URL传递,中间元素应该返回给用户.要从字符串中获取Content,它需要是"String",而不是"IO String"......如何轻松实现?
提前致谢!
我有一个包含的csv文件
"VANS, PASSENGER TYPE",CHEVROLET,H1500 EXPRESS AWD,5.3,8,Auto(L4),4,832,9,12,10,11.5,16.2,13.2268,E,,,,,,,,,,3900,310-340,CLKUP ,2,15,30-Jun-07,DERIVED
Run Code Online (Sandbox Code Playgroud)
我所要做的就是用逗号分隔,只做我当前的解决方案的问题是第一个条目碰巧"VANS, PASSENGER TYPE"有一个逗号,但我对分裂它不感兴趣.
目前我正在做这样的事情
with open("file.txt", "r") as ins:
foo = ins.split(",")
Run Code Online (Sandbox Code Playgroud) 我的文本文件看起来像这样:
1 2 3 test//test
4 5 6 dummy//dummy
Run Code Online (Sandbox Code Playgroud)
如何阅读此文件并仅处理每行中的数字?
我想打开一个文件,读取其内容,然后在文件中附加一行.我以为我应该使用"a +"标志来完成任务.
我有一个打开文件并返回指向该文件的指针的函数.
FILE* open_weekly_disk_file(char* filename){
FILE* weekly_log;
weekly_log = fopen(filename, "a+");
//weekly_log = fopen(filename, "r");
if(! weekly_log){
printf("The attempt to open the weekly log failed!\n");
return NULL;
} else{
return weekly_log;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个函数调用上面的函数并使用scanf从文件中读取内容:
void sample_function(char* filename){
FILE* log;
char token[100], current_read[100];
int limit;
log = opened_weekly_disk_file(filename);
// The problem happens here
for(limit=0; limit < TOKEN_NUMBER; limit++){
if(fscanf(log, "%s%s", &token, ¤t_read) == 2){
printf("%s %s\n", token, current_read);
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
我使用时此代码有效:
weekly_log = fopen(filename, "r");
Run Code Online (Sandbox Code Playgroud)
但是当我将"r"标志更改为"a +"时不起作用.我在for循环之前得到了一个Segmentation错误.
我有以下代码:
std::istream is;
// ... stream initialization ...
while(is)
{
uint32_t next4Bytes = 0;
is.read(reinterpret_cast<char*>(&next4Bytes), 4);
if(next4Bytes == 218893066)
{
is.seekg(-4, std::ios_base::cur);
break;
}
else
is.seekg(-3, std::ios_base::cur);
}
Run Code Online (Sandbox Code Playgroud)
有没有比reinterpret_cast<char*>从4个字节读std::istream入更好的方法uint32_t?(显然不是c式演员)
(编辑:除了header和main()函数的括号外,我没有排除任何代码.这里列出的代码行之间没有任何内容.)
.
我使用ReadFile函数来读取此COM3端口(它没有返回INVALID_HANDLE_VALUE或ERROR_FILE_NOT_FOUND):
LPCTSTR portName = "COM3" ;
HANDLE hSerial;
hSerial = CreateFile(portName,
GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // default security attributes
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL);
Run Code Online (Sandbox Code Playgroud)
并且有问题的ReadFile函数使用以下参数:
DWORD n = 512 ;
char szBuff[n] = {0};
DWORD dwBytesRead = 0;
if(!ReadFile(hSerial, szBuff, n, &dwBytesRead, NULL))
{
cout << "ReadFile error. Error code: " << GetLastError() << endl ;
cin.get() ;
return 0 ;
}
Run Code Online (Sandbox Code Playgroud)
我应该引入什么改变才能使读取成功?
(我搜索了函数的文档和其他StackOverflow问题,测试了很多东西,但找不到答案.)
我想逐行读取一个简单的文件,并将内容保存到一个数组中.如果我编译源代码,我会收到此错误:
test.C: In function ‘char* read_file(int, int, const char*)’:
test.C:14:11: error: cannot convert ‘char (*)[1024]’ to ‘char*’ in return
return words;
^
Run Code Online (Sandbox Code Playgroud)
这是什么意思?为什么我不能返回2D数组的指针?这是源代码.
#include <stdio.h>
#include <string.h>
char * read_file(int buf, int size, const char *fname){
char words[size][buf];
FILE *fptr = NULL;
int idx = 0;
fptr = fopen(fname, "r");
while(fgets(words[idx], buf, fptr)) {
words[idx][strlen(words[idx]) - 1] = '\0';
idx++;
}
return words;
}
int main(void) {
char * words;
words = read_file(1024, 100, "text.txt");
int i;
for(i = 0; …Run Code Online (Sandbox Code Playgroud)