我有一个特殊的问题.我需要使用python(从txt文件中)读取那些存在于预定义偏移范围内的子串.让我们说5-8和12-16.
例如,如果文件中的行是这样的:
abcdefghi akdhflskdhfhglskdjfhghsldk
Run Code Online (Sandbox Code Playgroud)
然后我想读两个词 - "efgh"和"kdhfl".因为,在单词"efgh"中,字符"e"的偏移量为5,"h"的偏移量为8.类似地,另一个单词"kdhfl".
请注意,空格也会增加偏移量.事实上,我文件中的空格在每一行都不是"一致的",并且不能依赖于提取感兴趣的单词.这就是为什么,我必须依靠补偿.
我希望我能够清楚地提出这个问题.
等待答案!
编辑 -
是的,每行中的空白量也可以改变并计算偏移量.例如,考虑这两行 -
abcz d
a bc d
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,我都将最终字符"d"的偏移视为相同.正如我所说,文件中的空格不一致,我不能依赖它们.我需要根据他们的偏移量来拾取角色.你的答案仍然有效吗?
我当时正在开始开发一个简单的十六进制编辑器(当时只能读取).我想替换OA的"\n",我使用此代码尝试:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main() {
ifstream infile;
int crtchar = (int)infile.get();
infile.open("test.txt", ifstream::in);
while(infile.good())
{
if(crtchar != 0xA)
cout << hex << setfill('0') << setw(2) << crtchar << ":";
else
cout << endl;
}
cout << "\n=====================================\n";
infile.close();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它编译时没有错误,但是当我尝试执行它时,我什么也没得到:
C:\ Documents and Settings\Nathan Campos\Desktop> hex
=====================================
C:\ Documents and Settings\Nathan Campos\Desktop>
这是刚刚发生的经过我已经添加了功能替代OA的\n,因为在此之前,它正在非常好.怎么了?
我多年没有写过任何Java了,我回过头来用一个简单的"从文件读取"的例子来刷新我的记忆.这是我的代码..
import java.io.*;
public class filereading {
public static void main(String[] args) {
File file = new File("C:\\file.txt");
FileInputStream fs = null;
BufferedInputStream bs = null;
DataInputStream ds = null;
try
{
fs = new FileInputStream(file);
bs = new BufferedInputStream(bs);
ds = new DataInputStream(ds);
while(ds.available()!= 0)
{
String readLine = ds.readLine();
System.out.println(readLine);
}
ds.close();
bs.close();
fs.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这编译得很好(尽管显然ds.readLine()已被删除),但在运行时,这给了我
在documentreading.main(filereading.java:21)的java.io.FilterInputStream.available(未知来源)中的线程"main"java.lang.NullPointerException中的异常
是什么赋予了?
我试图从服务器打开一个文件
我现在有
Dim attachedFilePath As String = "\\myserver\myshare\test.txt"
File.Open(attachedFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)
Run Code Online (Sandbox Code Playgroud)
这不会打开文件.
但是,如果我将路径更改为本地,则没有问题.
Dim attachedFilePath As String = "c:\...\test.txt"
Run Code Online (Sandbox Code Playgroud)
那么,有没有办法从远程存储打开文件?
好吧,我是VB.NET和桌面应用程序开发的新手.简化这是我的应用程序中发生的事情:
Dim Files() As New List(Of IO.FileInfo)
Files.Add( (New IO.FileInfo("C:\img1.jpg")) )
Files.Add( (New IO.FileInfo("C:\img2.jpg")) )
'Picture is a Windows.Forms.PictureBox in my WinForm '
Picture.Image = New System.Drawing.Bitmap(Files(0).FullName)
Picture.image = Nothing
CurrentFile = Files(0)
'Show next pic (img2)'
Files.RemoveAt(0)
Picture.Image = New System.Drawing.Bitmap(Files(0).FullName)
'Move img1 to other location '
CurrentFile.MoveTo("C:\other\img1.jpg")
Run Code Online (Sandbox Code Playgroud)
最后一行将抛出一个异常,说img1因为正在使用而无法移动.所以我的应用程序仍在使用它,但如何让我的应用程序停止锁定文件?没有什么可以保持它(据我所见)
为什么这段代码会产生警告?
FILE* my_open_file(char* filename)
{
FILE* fp=fopen(filename,"r");
if(fp==NULL)
{
perror("Error opening file");
return -1;
}
return fp;
}
Run Code Online (Sandbox Code Playgroud)
fp已经是一个指针,就我所见而言,它不是整数.
我有一个文件:
P 0.5 0.6 0.3
30 300
80 150
160 400
200 150
250 300
T
r 45 0 0
s 0.5 1.5 0 0
t 200 –150
.
.
.
Run Code Online (Sandbox Code Playgroud)
当我读到'P'时,我知道将会有3个花车.接下来是有限数量的X和Y坐标.数字会有所变化,直到达到"T",我必须承认.然后可能会有一个'r','s'或't'后跟一些值.
无论如何我知道如何识别'P'然后接收2个浮点数然后我知道我必须有一个while循环的X和Y坐标,当我到达'T'时它将停止.我不太了解C++使循环停止并识别'T'然后做其他事情.
一个例子来解释将不胜感激.提前致谢!
嗨,我已搜索周围,没有什么是相当我需要什么,我是可怕的用PHP迄今.
基本上,我有一个文本文件作为数据库.
每行都有以下形式:
id|lat|lng|details
Run Code Online (Sandbox Code Playgroud)
哪里:
id是唯一的整数,lat和lng是浮子和details是一个字符串.
我有一个客户端页面(在user-pass下锁定),其中用户输入唯一ID,PHP脚本应该删除文件中具有该唯一ID的行.
我该如何做到这一点?
谢谢,
我不明白为什么atoi()每个条目都工作,但第一个条目.我有以下代码来解析一个简单的.csv文件:
void ioReadSampleDataUsers(SocialNetwork *social, char *file) {
FILE *fp = fopen(file, "r");
if(!fp) {
perror("fopen");
exit(EXIT_FAILURE);
}
char line[BUFSIZ], *word, *buffer, name[30], address[35];
int ssn = 0, arg;
while(fgets(line, BUFSIZ, fp)) {
line[strlen(line) - 2] = '\0';
buffer = line;
arg = 1;
do {
word = strsep(&buffer, ";");
if(word) {
switch(arg) {
case 1:
printf("[%s] - (%d)\n", word, atoi(word));
ssn = atoi(word);
break;
case 2:
strcpy(name, word);
break;
case 3:
strcpy(address, word);
break;
}
arg++;
}
} …Run Code Online (Sandbox Code Playgroud) 我正在使用Apache Tomcat Server 6和Java 1.6,并试图将mp3大文件写入ServletOutputStream,以供用户下载。目前文件大小为50-750MB。
较小的文件不会引起太大的问题,但是较大的文件会导致套接字异常中断。
File fileMp3 = new File(objDownloadSong.getStrSongFolder() + "/" + strSongIdName);
FileInputStream fis = new FileInputStream(fileMp3);
response.setContentType("audio/mpeg");
response.setHeader("Content-Disposition", "attachment; filename=\"" + strSongName + ".mp3\";");
response.setContentLength((int) fileMp3.length());
OutputStream os = response.getOutputStream();
try {
int byteRead = 0;
while ((byteRead = fis.read()) != -1) {
os.write(byteRead);
}
os.flush();
} catch (Exception excp) {
downloadComplete = "-1";
excp.printStackTrace();
} finally {
os.close();
fis.close();
}
Run Code Online (Sandbox Code Playgroud)