我正在尝试在C中编写一个函数来获取文件的扩展名.所以这是我的代码
char *getFileNameExtension(char *filename){
char *ext = memchr(filename, '.',strlen(filename));
return ext;
}
Run Code Online (Sandbox Code Playgroud)
它工作正常.但是,当我试图检查特定的文件类型,如文本文件(.txt)时,我使用此代码
if (getFileNameExtension(filename)==".txt"){
printf("%s\n",filename);
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用.我试图将两个字符串打印为整数和字符串
printf("%s %s",".txt",getFileNameExtension(txtFile));
printf("%d %d",".txt",getFileNameExtension(txtFile));
Run Code Online (Sandbox Code Playgroud)
第一个输出是好的,我得到:.txt .txt,但是当我将它们打印为整数(我猜的是ascii代码)时,我得到了这个结果:27918619 4196714.此外,每次执行代码时,第一个字符串的int值都会更改(由于我猜的内存位置不同).有人可以帮忙吗?
我在我的C#应用程序中编写了一个函数,它应该在"0"到来时删除整行.但我不知道我的输出文本文件没有删除它.
我怎样才能解决这个问题.
代码段:
public void do_name()
{
string[] search_text = new string[] { "PCB", "DOC", "PCB1", "DOC1" };
string old;
StringBuilder sb = new StringBuilder();
using (StreamReader sr = File.OpenText(textBox1.Text))
{
while ((old = sr.ReadLine()) != null)
{
if (old.Contains(search_text[0]) || old.Contains(search_text[1]) ||
old.Contains(search_text[2]) || old.Contains(search_text[3]) ||
old.Split(" ".ToArray()).Equals("0"))
//here delete's the line where, "PCB", "DOC", "PCB1", "DOC1" is coming.
//but not '0"
continue;
else
sb.AppendLine(old);
}
sr.Close();
}
File.WriteAllText(textBox1.Text, sb.ToString());
}
Run Code Online (Sandbox Code Playgroud)
我的输入文本文件如下所示:
"CN5" "210-0141" "PHOENIX/8/150/V/F" "353.441" "115.951" …Run Code Online (Sandbox Code Playgroud) 我试图弄清楚如何从文本文件中删除所有逗号,以便我可以在没有逗号的控制台中输出它.我的input.replace("[,]", "");陈述不起作用,我不知道如何解决它.
我很感激你的帮助.
文本文件:
75 Fresco, Al
67 Dwyer, Barb
55 Turner, Paige
108 Peace, Warren
46 Richman, Mary A.
37 Ware, Crystal
83 Carr, Dusty
15 Sledd, Bob
64 Sutton, Oliver
70 Mellow, Marsha
29 Case, Justin
35 Time, Justin
8 Shorts, Jim
20 Morris, Hugh
25 Vader, Ella
76 Bird, Earl E.
Run Code Online (Sandbox Code Playgroud)
我的代码:
import java.io.*;
import java.util.*;
public class Ex2 {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("people.txt"));
while …Run Code Online (Sandbox Code Playgroud) 这是我的代码:
results = [[username, score]]
with open("hisEasyR.txt","a") as hisEasyRFile:
for result in results:
score = result[0]
username = result[1]
hisEasyRFile.write(score, '|' , username , '\n')
Run Code Online (Sandbox Code Playgroud)
我收到错误:
hisEasyRFile.write(score, '|' , username , '\n')
TypeError: function takes exactly 1 argument (4 given)
Run Code Online (Sandbox Code Playgroud)
知道为什么会这样吗?此外,'得分'是一个整数.这可能会影响它吗?我不相信它可以写整数到文件虽然是吗?我需要它是一个整数以供将来使用,但如果我需要将它转换为字符串,有没有办法在我读取文件后将其转换回整数?
import time
def taym():
time.sleep(5)
def getprice():
myfile = open('C:\\Users\\DELL\\Desktop\\Python\\html1.html')
txt = myfile.read()
t = txt.find("$")
it = float(txt[t-4:t])
it=8
while it != 1000:
getprice()
if it <= 4.74:
print("Price is Ok!")
taym()
else:
print("The price of the coffee beans is "+txt[t-4:t+1])
taym()
Run Code Online (Sandbox Code Playgroud)
当我在python3中运行此代码时,我收到如下错误消息:
"print("The price of the coffee beans is "+txt[t-4:t+1])
NameError: name 'txt' is not defined."
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用getprice()循环内部的原始代码,但是当我有一个我调用的函数时,我需要知道为什么它不起作用.
我想转换这个文本文件:
(516, 440)
(971, 443)
(1186, 439)
(1402, 441)
(1630, 449)
(299, 681)
(518, 684)
(736, 691)
(739, 431)
Run Code Online (Sandbox Code Playgroud)
进入一个看起来像这样的列表:
List = [
(516, 440),
(971, 443),
(1186, 439),
(1402, 441),
(1630, 449),
(299, 681),
(518, 684),
(736, 691),
(739, 431)
]
Run Code Online (Sandbox Code Playgroud)
由于括号 () 中的逗号,我在这里找到的答案对我不起作用。有人知道该怎么做吗?
我有一个文件夹.tif,我想使用python 将他们的文件名写入一个没有文件扩展名的文件.txt或.csv文件.这应该很简单,但由于某种原因,我总是以一个空的文本文件结束.任何人都可以在我的代码中看到我做错了吗?它正确打印名称,所以我知道.rstrip命令没有问题.
# import os so you get the os access methods
import os
# set a directory the files are in
workingDir = r'F:\filepath\files'
# get a list of all the files in the directory
names = os.listdir(workingDir)
#print file names
for name in names:
listname=name.rstrip('.tif')
print listname
#write filenames to text file
target = open("F:\filepath\list.txt", "w")
for name in names:
listname=name.rstrip('.tif')
target.writelines(listname)
target.writelines("\n")
target.close
Run Code Online (Sandbox Code Playgroud) 我正在阅读一个巨大的文本文件,如下所示.现在想要的是,如果我达到一个特定的单词test:在这里说,我将从该行推送到数据库的值,并通过连接10个后续行的值来输出一个值.所以我希望通过使用File. Readalllines或任何其他机制获得10个后续行并将其存储到内存中.我将如何实现它.截至目前,我正在加载整个文本文件,File.Readalllines这使得应用程序非常慢.
using (FileStream fs = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (BufferedStream bs = new BufferedStream(fs))
using (StreamReader sr = new StreamReader(bs))
{
string s = String.Empty;
while ((s = sr.ReadLine()) != null)
{
if (s.Contains("test:"))
{
//
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑
我正在研究消费者答案,这里有一个扭曲假设我有10000行开头,test:每行都有ID,我插入一个ID的行,如果ID == ID存储在字典中,那么我必须存储成功的10行而不是总共有前10行,那怎么办呢?
编辑
@spender我这样做SkipWhile(line => (!line.Contains("ADD GTRX:") && !line.Contains(dict.CellId.ToString()))).Take(10);但没有运气
编辑 @spender最后我做了它我必须移动括号如此完整代码我做了如下所示,我删除空行也
var linesOfInterest = File.ReadLines(filename)
.SkipWhile(line => !line.Contains("test:")).SkipWhile(line => !line.Contains(dictId.ToString())).Take(10);
var output = string.Join(Environment.NewLine, linesOfInterest).Trim().Split(';').Where(arg …Run Code Online (Sandbox Code Playgroud) 当我运行此代码时,lineLength 返回为 -1,当我希望换行符位于文本行字符串的末尾并返回一个相应的值时。如果 \n 字符实际上位于行首,那么如何找到该行中最后一个字符的值?
我尝试通过将 lineLength 设置为 line.Length 来使用 .length 但是当我将 lineLength 作为 line.substring 中的长度参数时,我收到一条错误消息:
“索引和长度必须指向字符串中的某个位置”
int found;
int lineLength;
string[] lines = System.IO.File.ReadAllLines(
@"C:\Program Files\C# Projects\Password Validation\UserAndPWSystem2\UPInfo.txt");
foreach (string line in lines)
{
if (line.Contains("user number : "))
{
found = line.IndexOf(":");
lineLength = line.IndexOf('\n');
Console.WriteLine(lineLength);
//Console.WriteLine(line.Substring(found + 2,lineLength));
}
}
Run Code Online (Sandbox Code Playgroud) 我对 C# 并不陌生,我一直都在使用字符串,但我不明白为什么在这种情况下我不能向简单的字符串添加一个简单的字符!我正在尝试将文本文件读入 C#,以便稍后将其插入 SQL 服务器表中。无论如何,我一直坚持向字符串添加单引号字符。我的代码如下:
string text;
using (var streamReader = new StreamReader(@"The_Directory\myTextFile.txt", Encoding.UTF8))
{
text = streamReader.ReadToEnd();
}
string[] lines = text.Split(new Char[] { '\n' });
string rearrange;
foreach (string line in lines)
{
rearrange = "*" + line + "'";
Console.WriteLine(rearrange);
}
Run Code Online (Sandbox Code Playgroud)
我得到的结果不包含我希望添加到字符串末尾的字符,而只包含我想添加到末尾的字符,在开头。
输出是这样的:
'The first line
'The second line
Run Code Online (Sandbox Code Playgroud)
我不明白为什么它不执行简单的字符串添加。我试图过滤掉行中任何可能的额外 \n 字符,但它没有帮助。