Vydělávej až 160.000 Kč měsíčně! Akreditované rekvalifikační kurzy s garancí práce od 0 Kč. Více informací.
Hledáme nové posily do ITnetwork týmu. Podívej se na volné pozice a přidej se do nejagilnější firmy na trhu - Více informací.
Avatar
trainmaniak
Člen
Avatar
trainmaniak:5.1.2014 21:06

Ahoj,
mám jeden problém. Mám program, který načte textový soubor do proměnné, přepíše proměnnou, a znovu uloží do toho souboru, přepíše. Jenže program skončí s chybou, že tento soubor je už používaný jiným programem (tímto). Prosím poraďte, co s tím

 
Odpovědět
5.1.2014 21:06
Avatar
Ondřej Štorc
Tvůrce
Avatar
Odpovídá na trainmaniak
Ondřej Štorc:5.1.2014 21:16

Asi by bylo dobré kdybys sem přidal kód

Nahoru Odpovědět
5.1.2014 21:16
Život je příliš krátký na to, abychom bezpečně odebírali USB z počítače..
Avatar
trainmaniak
Člen
Avatar
trainmaniak:5.1.2014 21:18

using System;
using System.Collec­tions.Generic;
using System.Linq;
using System.Text;
using System.Threadin­g.Tasks;
using System.IO;

namespace ConsoleApplica­tion22
{
class Program
{
static void Main(string[] args)
{

string file_name = "data.txt";

int num_lines = File.ReadAllLi­nes(file_name)­.Length;

string[] input = new string[num_lines];
string[] output = new string[num_lines];

while (1 < 2)
{

StreamReader sr_public = File.OpenText(fi­le_name);
using (StreamReader sr = File.OpenText(fi­le_name))
{

for (int i = 0; i < num_lines; i++)
{
string line = sr.ReadLine();
string[] line_split = line.Split(';');

input[i] = line_split[0];
output[i] = line_split[1];

}
}

string question = Console.ReadLine();
int index = Array.IndexOf(in­put, question);
if (index != -1)
{
Console.Write­Line(output[in­dex]);
}
else
{
Console.Write­Line(question);
string answer = Console.ReadLine();
string save = sr_public + question + ";" + answer;

using (StreamWriter sw = new StreamWriter(fi­le_name, true))
{
sw.WriteLine(sa­ve.Remove(0, 22));
}
}
}
}
}
}

 
Nahoru Odpovědět
5.1.2014 21:18
Avatar
Odpovídá na trainmaniak
Vojtěch Pospíchal:5.1.2014 21:32

Co to třeba zkusit pomocí tlačítka kódu? Kdo má tohle číst?

 
Nahoru Odpovědět
5.1.2014 21:32
Avatar
Jan Vargovský
Tvůrce
Avatar
Odpovídá na trainmaniak
Jan Vargovský:5.1.2014 21:37

Když otvíráš 2x ten samý soubor, nediv se. Nebo ho máš otevřený normalně na desktopu a otvíráš je ještě zkus tvůj program.

Editováno 5.1.2014 21:38
 
Nahoru Odpovědět
5.1.2014 21:37
Avatar
trainmaniak
Člen
Avatar
trainmaniak:5.1.2014 21:38
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApplication22
{
    class Program
    {
        static void Main(string[] args)
        {

            string file_name = "data.txt";


            int num_lines = File.ReadAllLines(file_name).Length;


            string[] input = new string[num_lines];
            string[] output = new string[num_lines];


            while (1 < 2)
            {

                StreamReader sr_public = File.OpenText(file_name);
                using (StreamReader sr = File.OpenText(file_name))
                {

                    for (int i = 0; i < num_lines; i++)
                    {
                        string line = sr.ReadLine();
                        string[] line_split = line.Split(';');


                        input[i] = line_split[0];
                        output[i] = line_split[1];

                    }
                }

                string question = Console.ReadLine();
                int index = Array.IndexOf(input, question);
                if (index != -1)
                {
                    Console.WriteLine(output[index]);
                }
                else
                {
                    Console.WriteLine(question);
                    string answer = Console.ReadLine();
                    string save = sr_public + question + ";" + answer;

                    using (StreamWriter sw = new StreamWriter(file_name, true))
                    {
                        sw.WriteLine(save.Remove(0, 22));
                    }
                }
            }
        }
    }
}
 
Nahoru Odpovědět
5.1.2014 21:38
Avatar
trainmaniak
Člen
Avatar
trainmaniak:5.1.2014 21:38

a jak ho zavřít po načtení?

 
Nahoru Odpovědět
5.1.2014 21:38
Avatar
Jan Vargovský
Tvůrce
Avatar
Odpovídá na trainmaniak
Jan Vargovský:5.1.2014 21:39
StreamReader sr_public = File.OpenText(file_name);
using (StreamReader sr = File.OpenText(file_name))

Mluvím o tomto...

 
Nahoru Odpovědět
5.1.2014 21:39
Avatar
trainmaniak
Člen
Avatar
trainmaniak:5.1.2014 21:43

už to funguje. Nevšiml jsem si toho :D
Díky

 
Nahoru Odpovědět
5.1.2014 21:43
Děláme co je v našich silách, aby byly zdejší diskuze co nejkvalitnější. Proto do nich také mohou přispívat pouze registrovaní členové. Pro zapojení do diskuze se přihlas. Pokud ještě nemáš účet, zaregistruj se, je to zdarma.

Zobrazeno 9 zpráv z 9.