小编Jav*_*oob的帖子

如何将Console.WriteLine输出保存到文本文件

我有一个程序,可以将各种结果输出到命令行控制台.

如何使用StreamReader其他技术将输出保存到文本文件?

System.Collections.Generic.IEnumerable<String> lines = File.ReadAllLines(@"C:\Test\ntfs8.txt");

foreach (String r in lines.Skip(1))
{
    String[] token = r.Split(',');
    String[] datetime = token[0].Split(' ');
    String timeText = datetime[4];
    String actions = token[2];
    Console.WriteLine("The time for this array is: " + timeText);
    Console.WriteLine(token[7]);
    Console.WriteLine(actions);
    MacActions(actions);
    x = 1;
    Console.WriteLine("================================================");
}

if (x == 2)
{
    Console.WriteLine("The selected time does not exist within the log files!");
}

System.IO.StreamReader reader = ;
string sRes = reader.ReadToEnd();
StreamWriter SW;
SW = File.CreateText("C:\\temp\\test.bodyfile");
SW.WriteLine(sRes);
SW.Close();
Console.WriteLine("File …
Run Code Online (Sandbox Code Playgroud)

c#

87
推荐指数
6
解决办法
17万
查看次数

C#如何将File.ReadLines转换为字符串数组?

我的问题是将读取行的过程从文本文件转换为数组而不是仅仅读取它.

我的代码中的错误出现在string[] lines = File.ReadLines("c:\\file.txt");with with implicity转换....

有人可以建议代码以数组格式保存结果吗?我已经放置了ReadAllLines代码,它能够将结果保存在数组中.谢谢!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace Testing
{
class Analysis
{
    static void Main()
    {
        string[] lines = File.ReadLines("c:\\file.txt");

        foreach (string r in lines)
        {
            Console.WriteLine("-- {0}", r);
        }

        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
}
}
Run Code Online (Sandbox Code Playgroud)

ReadAllLines代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace Testing
{ …
Run Code Online (Sandbox Code Playgroud)

c# file

22
推荐指数
2
解决办法
10万
查看次数

如何在Python中获取网络接口卡名称?

我是python编程的新手,所以请耐心等待我.

无论如何要在机器等中获取NIC卡的名称eth0,lo?如果是这样你怎么做?

我已经研究过但到目前为止我只找到了代码来获取IP地址和MAC地址,例如

import socket
socket.gethostbyname(socket.gethostname())
Run Code Online (Sandbox Code Playgroud)

对这些代码的建议真的很感激.谢谢!

python networking

21
推荐指数
8
解决办法
4万
查看次数

C#如何使用Stream Reader读取文本文件时跳过行数?

我有一个程序,它读取一个文本文件并处理它分成几个部分.

所以问题是如何更改程序以允许程序在使用Stream Reader读取文件时跳过读取文件的前5行?

有人可以告知代码吗?谢谢!

代码:

class Program
{
    static void Main(string[] args)
    {
        TextReader tr = new StreamReader(@"C:\Test\new.txt");

        String SplitBy = "----------------------------------------";

        // Skip first 5 lines of the text file?
        String fullLog = tr.ReadToEnd();

        String[] sections = fullLog.Split(new string[] { SplitBy }, StringSplitOptions.None);

        //String[] lines = sections.Skip(5).ToArray();

        foreach (String r in sections)
        {
            Console.WriteLine(r);
            Console.WriteLine("============================================================");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# lines

19
推荐指数
4
解决办法
4万
查看次数

如何在Python中解析DNS?

我有一个DNS脚本,允许用户通过在Windows命令提示符下键入网站名称来解析DNS名称.

我查看了有关DNS解析的几个指南,但我的脚本似乎无法将名称(www.google.com)或(google.com)解析为IP地址.

该脚本输出错误

Traceback (most recent call last):
  File "C:\python\main_menu.py", line 37, in ?
    execfile('C:\python\showdns.py')
  File "C:\python\showdns.py", line 3, in ?
    x = input ("\nPlease enter a domain name that you wish to translate: ")
  File "<string>", line 0, in ?
NameError: name 'google' is not defined
Run Code Online (Sandbox Code Playgroud)

代码:

import socket

x = input ("\nPlease enter a domain name that you wish to translate: ")

print ("\n\nThe IP Address of the Domain Name is: "+socket.gethostbyname_ex(x))

x = raw_input("\nSelect enter to proceed …
Run Code Online (Sandbox Code Playgroud)

python reverse-dns

14
推荐指数
2
解决办法
4万
查看次数

如何删除文本文件中的最后一行?

我有.txt的扩展与从第三方程序生成的日志文件,每次该文本文件的最后一个空白行一个简单的日志文本文件.

因此,我可以使用任何方法或代码来删除文本文件的最后一行吗?

日志文本文件的示例:

Sun Jul 22 2001 02:37:46,73882,...b,r/rrwxrwxrwx,0,0,516-128-3,C:/WINDOWS/Help/digiras.chm
Sun Jul 22 2001 02:44:18,10483,...b,r/rrwxrwxrwx,0,0,480-128-3,C:/WINDOWS/Help/cyycoins.chm
Sun Jul 22 2001 02:45:32,10743,...b,r/rrwxrwxrwx,0,0,482-128-3,C:/WINDOWS/Help/cyzcoins.chm
Sun Jul 22 2001 04:26:14,174020,...b,r/rrwxrwxrwx,0,0,798-128-3,C:/WINDOWS/system32/spool/drivers/color/kodak_dc.icm
Run Code Online (Sandbox Code Playgroud)

c# line

10
推荐指数
3
解决办法
3万
查看次数

C#如何将目录空白空间用于process.arguements?

创建的程序使用第三方工具生成日志文件.

但是,为该工具提供的参数需要目录位置中的各种文件作为生成日志的一部分.因此,主要参数@"-r C:\test\ftk\ntuser.dat -d C:\System Volume Information\" + restoreFolder.Name + " -p runmru";将用于生成日志.

有人可以建议如何使系统处理"C:\ System Volume Information"的参数并放置空格?谢谢!

代码:

            Process process = new Process();
            process.StartInfo.FileName = @"C:\test\ftk\ripxp\ripxp.exe";
            process.StartInfo.Arguments = @"-r C:\test\ftk\ntuser.dat -d C:\System Volume Information\" + restoreFolder.Name + " -p runmru";
            process.StartInfo.CreateNoWindow = false;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardError = true;
            process.Start();
Run Code Online (Sandbox Code Playgroud)

c# directory arguments process

7
推荐指数
1
解决办法
1万
查看次数

如何将AWK输出传递给变量?

我有一个小的bash脚本,通过使用关键字greps/awk段落.

但在添加额外的代码后:set var = "(......)"它只打印一个空行而不是段落.

所以我想问一下是否有人知道如何正确地将awk输出传递给变量进行输出?

我的代码:

#!/bin/sh

set var = "(awk 'BEGIN{RS=ORS="\n\n";FS=OFS="\n"}/FileHeader/' /root/Desktop
/logs/Default.log)"
echo $var;
Run Code Online (Sandbox Code Playgroud)

谢谢!

linux awk paragraph

6
推荐指数
1
解决办法
3万
查看次数

如何使用Perl对文本文件进行grep和排序?

我有一个简单的日志文件非常混乱,我需要它整洁.该文件包含日志标题,但它们都混杂在一起.因此,我需要根据日志标题对日志文件进行排序.没有静态行数 - 这意味着文本文件的每个标题没有固定数量的行.我正在使用perl grep来整理标题.

日志文件是这样的:

Car LogFile Header
<text>
<text>
<text>
Car LogFile Header
<text>
Car LogFile Header
<and so forth>
Run Code Online (Sandbox Code Playgroud)

我已经完成/搜索了一个简单的算法,但它似乎没有工作.有人可以指导我吗?谢谢!

#!/usr/bin/perl

#use 5.010; # must be present to import the new 5.10 functions, notice 
#that it is 5.010 not 5.10


my $srce = "./root/Desktop/logs/Default.log";
my $string1 = "Car LogFile Header";
open(FH, $srce);
my @buf = <FH>;
close(FH);
my @lines = grep (/$string1/, @buffer);
Run Code Online (Sandbox Code Playgroud)

执行代码后,终端没有显示结果.有任何想法吗?

perl grep text

5
推荐指数
1
解决办法
3万
查看次数

如何在C#中将单词的字母拆分为数组?

如何将字符串拆分为C#中的字符数组?

示例使用的字符串字是"机器人".

该程序应打印出来:

r
o
b
o
t
Run Code Online (Sandbox Code Playgroud)

原始代码段:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;


namespace Testing
{
class Program
{
    static void Main(string[] args)
    {
        String word = "robot";

        String[] token = word.Split(); // Something should be placed into the () to 
        //print letter by letter??

        foreach (String r in token)
        {
            Console.WriteLine(r);
        }
    }
}
}
Run Code Online (Sandbox Code Playgroud)

如何正确实施代码?

.net c# string tokenize

5
推荐指数
2
解决办法
4万
查看次数

标签 统计

c# ×6

python ×2

.net ×1

arguments ×1

awk ×1

directory ×1

file ×1

grep ×1

line ×1

lines ×1

linux ×1

networking ×1

paragraph ×1

perl ×1

process ×1

reverse-dns ×1

string ×1

text ×1

tokenize ×1