标签: file

不会写入文本文件

我已经创建了一个表单.如果接收到所有信息并且似乎工作,但是当我检查文本文件时没有写入任何内容,并且它只允许我在错误输出之前运行表单两次.有谁看到了问题?

const string FileName = "Friends.txt";
Friend friend = new Friend();
FileStream file = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite);
FileStream file2 = new FileStream(FileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); 

public Form1()
{
    InitializeComponent();
}

private void enter_Click(object sender, EventArgs e)
{
    StreamWriter write = new StreamWriter(file2);

    try
    {
        friend.FirstName = firstName.Text;
        friend.LastName = lastName.Text;
        friend.PhoneNumber = phoneNumber.Text;
        friend.Month = Convert.ToInt32(birthMonth.Text);
        friend.Day = Convert.ToInt32(birthday.Text);
        write.WriteLine(friend.ToString());
        MessageBox.Show("Wrote " + friend.ToString() + " to file.");
    }
    catch(Exception error)
    {
        MessageBox.Show(error.Message + " Please reenter the …
Run Code Online (Sandbox Code Playgroud)

c# file filestream streamwriter winforms

0
推荐指数
1
解决办法
609
查看次数

Python if语句不起作用

我在python中有一个if/else语句,当它转到else语句时我想什么都不做.

我正在读一个单词列表,找到回文(例如'abba')并打印出来.它目前打印出整个单词列表,我知道is_palindrome函数正常工作.

def first(word):
    return word[0]

def last(word):
    return word[-1]

def middle(word):
    return word[1:-1]


def is_palindrome(word):
    #print(word)
    if len(word) >1:
        if first(word) == last(word):
            #print(word)
            #print(middle(word))
            return is_palindrome(middle(word))
        else:
            return False
    else:
        return True


try:
    words = open("/usr/share/dict/words","r")

    for line in words:
        line.strip()
        #print line
        if is_palindrome(line) == True:
            print(line)
    else
    words.close()
except:
    print("File open FAILED")
Run Code Online (Sandbox Code Playgroud)

我很感激你能给我的任何见解.谢谢.

python if-statement file

0
推荐指数
1
解决办法
248
查看次数

如何将16位整数写入文件?

使用C++,Windows 7,Intel CPU.

我想要做的是将浮点值[-1,1]映射到16位有符号值并将它们写入文件.最明显的事情似乎是将浮点值乘以32768(2 ^ 16/2),然后简单地写它们.以下是我这样做时会发生的事情:

    std::ofstream outfile(filename.c_str());
    float hypotheticalFloat = 0.25;
    int16_t scaledVal = hypotheticalFloat*32768;
    outfile << scaledVal;
Run Code Online (Sandbox Code Playgroud)

然后八进制转储命令告诉我,我有

    $ od -cd output.pcm
    0000000   8   1   9   2
              12600   12857
Run Code Online (Sandbox Code Playgroud)

在我看来,它将每个int16_t值的数字写为自己的字节.我会感激任何知道这里发生了什么的人.我不知所措.

c++ windows file

0
推荐指数
1
解决办法
1271
查看次数

为什么我的while循环不会停止?

我不知道为什么我的循环无限循环.

URLLinks

public String findFile() {
    try (BufferedReader br = new BufferedReader(
            new FileReader("urls.txt"))) {

        String sCurrentLine;

        if ((sCurrentLine = br.readLine()) != null) {
            System.out.println(sCurrentLine);

            break;
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    return findFile();

}
Run Code Online (Sandbox Code Playgroud)

我只想打印一次sCurrentLine,但它会无限打印出来.我认为问题在于返回findFile(),但我不知道如何修复它.

干杯

再次更新UPDATE UPDATE

我通过将方法设置findFile()void,并从while循环更改为if语句来修复问题

URLLinks更新代码

public void findFile() {
    try (BufferedReader br = new BufferedReader(
            new FileReader("urls.txt"))) {

        String sCurrentLine;

        while ((sCurrentLine = br.readLine()) != null) {
            System.out.println(sCurrentLine);

        }

    } catch (IOException e) {
        e.printStackTrace();
    } …
Run Code Online (Sandbox Code Playgroud)

java file while-loop

0
推荐指数
1
解决办法
544
查看次数

将txt文件转换为字典<string,string>

我有一个文本文件,我需要将所有偶数行放到Dictionary Key和所有偶数行到Dictionary Value.什么是我的问题的最佳解决方案?

int count_lines = 1;
Dictionary<string, string> stroka = new Dictionary<string, string>();

foreach (string line in ReadLineFromFile(readFile))
{
    if (count_lines % 2 == 0)
    {
        stroka.Add Value
    }
    else
    { 
       stroka.Add Key
    }

    count_lines++;
}
Run Code Online (Sandbox Code Playgroud)

c# dictionary file

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

从文本文件Ping服务器

我正在尝试ping大约20-30个文本文件上的服务器并且可以相应地更新(服务器不断更改名称或变得过时).而且我不想将服务器放在批处理文件中,每次发生变化时都必须对其进行编辑.

但我的询问是:如何从.txt文件中ping一组服务器,并在单独的.txt文件(我们称之为"Site_A_Servers.txt")上输出结果(如果它在线或不在线):

Site_A_Servers.txt:
Server A is online.
Server B is online.
Server C is offline!
Server D is etc..

感谢您的时间!:)

text file ping batch-file

0
推荐指数
1
解决办法
9823
查看次数

尝试上传文件aspx/C时出错#

我有以下C#代码:

string File = "../images/main/profile-icon.png";
if (Request.ContentLength != 0)
{
    int Size = Request.Files[0].ContentLength / 1024;
    if (Size <= 512)
    {

        string LocalFile = Request.Files[0].FileName;
        int dot = LocalFile.LastIndexOf('.');
        string FileType = LocalFile.Substring(dot + 1);
        if (FileType == "gif" || FileType == "jpg" || FileType == "png" || FileType == "GIF" || FileType == "JPG" || FileType == "PNG")
        {
            int LastIndex = LocalFile.LastIndexOf(@"\") + 1;
             File = LocalFile.Substring(LastIndex, LocalFile.Length - LastIndex);
            File = DateTime.Today.ToString();
            string Path = Server.MapPath(" ../images/profiles") …
Run Code Online (Sandbox Code Playgroud)

c# asp.net file-upload file

0
推荐指数
1
解决办法
384
查看次数

C:没有这样的文件或目录

当我硬编码cheminopen(chemin, O_RDONLY)一个文件名,程序工作,但是当我离开,如果给open(chemin, O_RDONLY)我弄No such file or directory.

为什么不chemin使用type_fichier?

当我使用printf("%s", chemin)type_fichier我得到'

int type_fichier(char * chemin) {
  int fp;
  if ((fp = open(chemin, O_RDONLY)) == -1) { perror(""); exit(0); }

  struct stat fileStat;
  if(fstat(fp, &fileStat) < 0)
    return 1;

  switch(fileStat.st_mode & S_IFMT) {
    case S_IFBLK:  printf("block device\n");            break;
    case S_IFCHR:  printf("character device\n");        break;
    case S_IFDIR:  printf("directory\n");               break;
    case S_IFIFO:  printf("FIFO/pipe\n");               break;
    case S_IFLNK:  printf("symlink\n");                 break;
    case S_IFREG: …
Run Code Online (Sandbox Code Playgroud)

c file mode file-descriptor

0
推荐指数
1
解决办法
2390
查看次数

在java中舍入为double

我有一个存储在文本文件中的双值对角矩阵.

size(file)~410 Mo
Run Code Online (Sandbox Code Playgroud)

我想通过舍入我的双值来减小大小.

如果它是一个好主意,如何在java中做到这一点

0.1706524958886193=>0.17
Run Code Online (Sandbox Code Playgroud)

我需要稍后在matlab中使用这个文件

当我尝试

dlmread(文件)我出现内存不足错误

java double matlab file rounding

0
推荐指数
1
解决办法
445
查看次数

读取大文件,文件大小错误

我正在尝试从磁盘读取大文件并在加载时报告百分比.问题是FileInfo.Length报告的大小不同于我的Encoding.ASCII.GetBytes().长度.

    public void loadList()
    {
        string ListPath = InnerConfig.dataDirectory + core.operation[operationID].Operation.Trim() + "/List.txt";
        FileInfo f = new FileInfo(ListPath);

        int bytesLoaded = 0;

        using (FileStream fs = File.Open(ListPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        using (BufferedStream bs = new BufferedStream(fs))
        using (StreamReader sr = new StreamReader(bs))
        {
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                byte[] array = Encoding.ASCII.GetBytes(line);
                bytesLoaded += array.Length;
            }
        }

        MessageBox.Show(bytesLoaded + "/" + f.Length);
    }
Run Code Online (Sandbox Code Playgroud)

结果是

    13357/15251
Run Code Online (Sandbox Code Playgroud)

有1900个字节'缺失'.该文件包含短字符串列表.有什么提示为什么它报告不同的文件大小?是否必须对文件中的'\ r'和'\n'字符执行任何操作?另外,我有以下几行:

    int bytesLoaded = 0;
Run Code Online (Sandbox Code Playgroud)

如果文件是1GB大,我是否必须使用'long'代替?感谢您的时间!

c# file

0
推荐指数
1
解决办法
977
查看次数