Stack Overflow 的用户您好!
我在尝试用 C++ 编写的程序遇到问题。每次编译代码时,都会出现以下错误:
FCS2Phase.cpp: In function `int main(int, char**)':
FCS2Phase.cpp:47:15: error: request for member `open' in `userfile', which is of non-class type `std::ifstream()'
FCS2Phase.cpp:48:22: error: request for member `eof' in `userfile', which is of non-class type `std::ifstream()'
FCS2Phase.cpp:50:39: error: cannot convert `std::ifstream (*)()' to `char**' for argument `1' to `ssize_t getline(char**, size_t*, FILE*)'
FCS2Phase.cpp:52:49: error: cannot convert `std::ifstream (*)()' to `char**' for argument `1' to `ssize_t getline(char**, size_t*, FILE*)'
FCS2Phase.cpp:54:14: error: request for member `close' in `userfile', …Run Code Online (Sandbox Code Playgroud) 这段代码的目标是运行四个线程,这将打开四个文本文件,从中读取单词,然后将它们放入一个字符串数组中,
我知道的主要问题:
1-我没有将并发函数放在 void run 函数中,我希望能够将参数传递给该函数
2-我不确定我是否正在修改字符串的全局数组
首先是主要方法:
public static void main(String[] args) throws IOException
{
//declare the threads
Thread thread1 = new Thread(ReadFile("list1.txt", Global.array1,"thread1"));
Thread thread2 = new Thread(ReadFile("list2.txt", Global.array2,"thread2"));
Thread thread3 = new Thread(ReadFile("list3.txt", Global.array3,"thread1"));
Thread thread4 = new Thread(ReadFile("list4.txt", Global.array4,"thread2"));
/*error message from netbeans: cannot find symbol
symbol: method ReadFile(java.lang.String,java.lang.String[])
it says it for every delcaration of the thread*/
thread1.start(); //putting the threads to work
thread2.start();
thread3.start();
thread4.start();
thread1.join(); //telling the threads to finish their work
thread2.join(); …Run Code Online (Sandbox Code Playgroud) 什么时候关闭文件?
# Read all data from input file
mergeData = open( "myinput.txt","r" )
allData = mergeData.read()
mergeData.close()
Run Code Online (Sandbox Code Playgroud)
我可以替换这个代码吗?
allData = read.open( "myinput.txt","r" )
Run Code Online (Sandbox Code Playgroud)
我想知道什么时候文件会被关闭?会在语句运行时关闭吗?或等到程序退出。
是否可以让python只从文件中读取选定的行?
比方说,我有一个CSV文件,文件被分开tab和第三列要么是“A”,“B”或“C”。我想要一个列表理解(或生成器,无关紧要)它只会返回文件中选择第一列的那些行
下面抛出一个语法错误:
lines = [tmp = line.rstrip().split(separator_column) for line in source if tmp[2] == 'a']
Run Code Online (Sandbox Code Playgroud)
是否有可能以比 for 循环更 Pythonic 的方式来完成?所谓的更多 Pythonic 方法正在以 C 的速度工作——它们比基本的 Python 指令更快——这就是我问的原因。
我有一个格式的文本文件:
2
1 3
2 4
Run Code Online (Sandbox Code Playgroud)
我编写了一个 python 程序,它要求输入的数量并打印出值 sum 。
n = int(raw_input())
for _ in range(n):
a,b = map(int,raw_input().split())
print a+b
Run Code Online (Sandbox Code Playgroud)
但
我正在尝试创建和打开一个文件fopen并fdopen写入一些内容。下面是我写的代码:
char Path[100];
int write_fd;
snprintf(Path,100,"%s/%s","/home/user","myfile.txt");
printf("opening file..\n");
write_fd = open(Path, O_WRONLY | O_CREAT | O_EXCL, 0777);
if(write_fd!=-1)
{
printf(" write_fd!=-1\n");
FILE *file_fp = fdopen(write_fd,"a+");
if (file_fp == NULL)
{
printf("Could not open file.File pointer error %s\n", std::strerror(errno));
close(write_fd);
return 0;
}
write(write_fd, "First\n", 7);
write(write_fd, "Second\n", 8);
write(write_fd, "Third\n", 7);
fclose(file_fp);
}
Run Code Online (Sandbox Code Playgroud)
文件 fdwrite_fd是使用 WRONGLY 权限创建的,该权限应该具有读/写(?)的权限。但是当fdopen使用 mode 调用文件描述符时a+,它会抛出错误,说无效参数。
它以 mode 成功打开a。
模式之间究竟有何不同a并a+导致此错误?
我在 Windows 7 上使用 Python 3.4。我的程序生成一些数字(范围 0-255),然后将它们转换为 ascii 字符 (chr) 并创建一个字符串。现在我想将此字符串的内容保存在文本文件中。它给了我以下错误:
UnicodeEncodeError: 'charmap' codec can't encode character '\x8e' in position 6: character maps to <undefined>
Run Code Online (Sandbox Code Playgroud)
请注意,字符串的长度是可变的,任何和所有代码 (0-255) 都可能出现。
示例代码:
file = open('somefiliename.txt', 'w')
file.write(result) #result being the string variable containing ascii chars.
file.close()
Run Code Online (Sandbox Code Playgroud)
我可以打印结果字符串,并且使用 print(result) 没有错误。但它没有保存到文件。
结果 = '' for y in range(4): for x in range(4): result += chr(matrix[x, y]) print(result)
代码很长,我在相关的上面添加了。matrix 是一个 numpy 二维 (4x4) 矩阵,用于存储数字。
我正在使用writeLines写入文件,但它似乎覆盖了前一行.最快/最喜欢的方法是什么?
使用writeLines
fd <- file("foo.Rmd")
writeLines('first line', fd)
writeLines('second line', fd)
close(fd)
$ cat foo.Rmd
second line
Run Code Online (Sandbox Code Playgroud)
用写
fd <- file("foo.Rmd")
write("foo", fd)
write("boo", fd, append=TRUE)
close(fd)
$ cat foo.Rmd
boo
Run Code Online (Sandbox Code Playgroud) 我正在尝试编辑名称中没有模式的文件夹中的100个.mat文件.每个文件都包含大小的矩阵100 -by- 10,我想将它们转换为10 -by- 10 -by- 10.我该怎么做?
由于某种原因,我的代码拒绝转换为大写,我无法弄清楚为什么.我试图将字典写入一个文件,其中大写字典值被输入到一种模板文件中.
#!/usr/bin/env python3
import fileinput
from collections import Counter
#take every word from a file and put into dictionary
newDict = {}
dict2 = {}
with open('words.txt', 'r') as f:
for line in f:
k,v = line.strip().split(' ')
newDict[k.strip()] = v.strip()
print(newDict)
choice = input('Enter 1 for all uppercase keys or 2 for all lowercase, 3 for capitalized case or 0 for unchanged \n')
print("Your choice was " + choice)
if choice == 1:
for k,v in newDict.items():
newDict.update({k.upper(): v.upper()}) …Run Code Online (Sandbox Code Playgroud) file-io ×10
python ×5
c++ ×2
arrays ×1
ascii ×1
dictionary ×1
file ×1
ifstream ×1
java ×1
matlab ×1
matrix ×1
python-2.7 ×1
python-3.x ×1
r ×1
windows-7 ×1