我有一些带有时间信息的文本文件,例如:
46321882696937;46322241663603;358966666
46325844895266;46326074026933;229131667
46417974251902;46418206896898;232644996
46422760835237;46423223321897;462486660
Run Code Online (Sandbox Code Playgroud)
现在,我需要文件的第三列来计算平均值.
我怎样才能做到这一点?我需要获取每个文本行,然后获取最后一列?
这是我的代码:
public static bool createFile(string dir) {
dir="c:\\e.bat";
System.IO.File.Create(dir);
if (System.IO.File.Exists(dir))
{
try
{
StreamWriter SW;
SW = System.IO.File.CreateText(dir);
SW.WriteLine("something ");
SW.Close();
}
catch (Exception e)
{
Console.Write(e.Message);
Console.ReadLine();
return false;
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
这里dir是当前目录.我正面临错误进程无法访问该文件,因为它被另一个进程使用.我可以解决这个问题吗?
例如:
FileWriter saveFile = new FileWriter("text.txt");
Run Code Online (Sandbox Code Playgroud)
此代码显示错误:
Unhandled exception type IOException
Run Code Online (Sandbox Code Playgroud)
这是什么意思?
我在C中编写了这个代码,一个接一个地在一个txt文件中打印1到10的nos但是在执行之后只有第一个no打印在txt文件中.请帮助
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i;
FILE *fptr;
fptr=fopen("C:\\program.txt","w");
for(i=1;i<=10;i++)
{
fprintf(fptr,"\n%d\n",i);
fclose(fptr);
}
}
Run Code Online (Sandbox Code Playgroud) 有人可以解释一下,为什么文件开放不成功?为什么打印"文件"会给-1?这有问题char *source吗?
int opf(char *source){
int file;
file=open(source,O_RWR);
printf("%d",file);
}
Run Code Online (Sandbox Code Playgroud)
是否可以这样做:文件在另一个目录中,所以
int opf(char *source){
int file;
file=open("some_directory/ %s",source,O_RWR);
printf("%d",file);
}
Run Code Online (Sandbox Code Playgroud)
在这里,我得到"从没有强制转换的指针生成整数"错误.我尝试了很多不同的东西,但我想问题在于我没有正确掌握这些概念.
open()的返回值为-1,在尝试使用perror()查找错误时,输出为"File exists".
如何查找错误或文件未打开的原因.
如果一个常量被声明为这样
const char *http_range;
Run Code Online (Sandbox Code Playgroud)
那我怎么能在文本文件中写出它的内容或价值呢?你能告诉我语法吗?
我想知道是否有一种C++方式打开文件并逐行读取输入.
我遇到了以下代码来完成任务:
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ifstream myfile;
myfile.open ("example.txt");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我鼓励不要使用任何C函数或命令.
问题是,我的"example.txt"是一个字符串的形式,并使用str.c_str()是一个C函数,所以我想我有两种方法来解决这个问题.
有没有其他方法可以逐行读取文件中的输入?也许使用一些接受字符串作为文件路径参数的东西?有没有C++的做事方式?:)
或者,是否有另一种方法将字符串转换为const char*,这是myfile.open()函数需要的?
提前谢谢了!
编辑:我缺乏实践和研究使我认为c_str()是一个C函数,而事实并非如此.我很抱歉.既然不是我找到了答案.
我写了一个运动检测winform c#desktop app.
运动帧作为单独的jpeg保存到我的硬盘驱动器中.
我记录了4台摄像机.这由变量表示:
camIndex
Run Code Online (Sandbox Code Playgroud)
每个jpeg都在一个文件结构下:
c:\ Year\The Month\The Day\The Hour\The Minute
确保目录中的每个文件都没有太多文件.
我的目的是让我的应用程序全天候运行.应用程序可能会因系统重启或用户选择暂时关闭它等原因而停止.
目前我有一个计时器而不是每5分钟运行一次,以删除超过24小时的文件.
我发现我的以下代码是内存密集型的,并且在几天的时间内,explorer.exe已经爬进RAM内存.
我的动作应用程序需要一直处于打开状态,因此低内存占用对于这种"归档"至关重要......
以下代码很长,在我看来非常低效.有没有更好的方法来实现我的目标?
我用这个代码:
List<string> catDirs = Directory.EnumerateDirectories(Shared.MOTION_DIRECTORY, "*", SearchOption.TopDirectoryOnly).ToList();
for (int index = 0; index < catDirs.Count; index++)
{
for (int camIndex = 0; camIndex < 4; camIndex++)
{
if (Directory.Exists(catDirs[index] + "\\Catalogues\\" + camIndex.ToString()))
{
List<string> years = GetDirectoryList(catDirs[index] + "\\Catalogues\\" + camIndex.ToString(), true);
if (years.Count == 0)
{
Directory.Delete(catDirs[index]);
}
for (int yearIndex = 0; yearIndex < years.Count; yearIndex++)
{
DirectoryInfo …Run Code Online (Sandbox Code Playgroud) 我有一个简历建筑工地.在哪个用户输入他们的信息,最后我们需要在doc或pdf文件中创建他的简历.
有人帮我请...
为什么这两个函数的 print len() 值不同?它们不一样吗?
此脚本打开的文件是一个包含三行文本的文本文件。我将它命名为 test.txt,里面是
Jack and Jill
gave up
they went home with no water
Run Code Online (Sandbox Code Playgroud)
代码:
def function2nd (filename):
target = open(theFile, 'r')
inData = target.read()
print inData
print len(inData)
target.close()
theFile = raw_input("What is the file name?\n>>")
function2nd(theFile)
def function3rd (filename):
target = open(theFile, 'r')
target.read()
print target.read()
print len(target.read())
target.close()
function3rd(theFile)
Run Code Online (Sandbox Code Playgroud) 这应该将我的输入保存到另一个txt文件.程序将询问您的txt文件的名称.
FILE *save;
char filename[100];
scanf ("%s", filename);
save = fopen ("filename", "w");
current = head;
while (current != NULL)
{
fprintf (save, " %s %s %d", current -> name, current -> tel, current -> age);
current = current -> next;
}
fclose(save);
Run Code Online (Sandbox Code Playgroud)
谁能告诉我我的错误?
如何在perl中的不同数组元素中存储不同行的文本文件
例如文本文件包含
US
London
Run Code Online (Sandbox Code Playgroud)
如何将美国存储在数组[0]和伦敦数组[1]中