I need to fill a text file with information about workers. Then I need to read from the file and search for an ID that user tries to find. For example my file contains ids 1,2,3 and if I try to find id 3 and it matches, then this worker's all information is written in console. Otherwise it writes a text A worker cannot be found.
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
string file = "C:\\Temp\\registery.txt";
FileStream fOutStream = File.Open(file, FileMode.Append, FileAccess.Write);
StreamWriter sWriter = new StreamWriter(fOutStream);
int[] id = { 1, 2, 3 };
string[] name = { "John", "Carl", "Thomas" };
float[] salary = { 3500, 4800, 2100 };
for (int i = 0; i < id.Length; i++)
{
sWriter.WriteLine(id[i] + " " + name[i] + " " + salary[i]);
}
sWriter.Flush();
sWriter.Close();
FileStream fInStream = File.OpenRead(file);
StreamReader sReader = new StreamReader(fInStream);
int id2;
Console.WriteLine("Type worker's id");
id2 = int.Parse(Console.ReadLine());
bool a;
a = sReader.ReadToEnd().Contains(id2);
Console.WriteLine(a);
sReader.Close();
}
}
If you want to create a text file to be searchable, it should be delimited by a separator like comma /TAB
so modify your code:
sWriter.WriteLine(id[i] + "," + name[i] + "," + salary[i]);
To search your text file by id/name/..whatever and use AND/OR, you can use the method described here:
How would I convert data in a .txt file into xml? c#
BTW: Re-factor your code to create the file in a separate method, and the search in other one.
I found a solution myself to my problem and it worked good enough. It might not be the best solution. I removed bool things and I replaced the whole thing with this:
string line;
while ((line = sReader.ReadLine()) != null)
{
if (line.Contains("id: " + id2))
{
Console.WriteLine(line);
break;
}
else if ((line = sReader.ReadLine()) == null)
{
Console.WriteLine("Worker not found with id " + id2);
}
}
And I fixed the upper for loop to look like this:
sWriter.WriteLine("id: " + id[i] + " name: " + name[i] + " salary: " + salary[i]);
Related
I've been writing below code to find and replace the wrong values with right one.
I've two files here, Source=Delimited text file like below.
J48309A0580113A27E053A2DEF40AC8B8,Z9e578475,
7e241974c714459997e20fe6e195ffb1,BD17946 and BD38168,
and my destination xmlJRN file looks like below. (multiple xmlJRN files)
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE eGAD SYSTEM "eGAD.Dtd">
<eGAD pakUID="PID_77F0F469AFE61B4A8121AA84EB349B6C">
<document docID="1" docMasterID="1A5D331DDF8A84385F77B621C3F9CD86" docInstanceID="49E3D61E9CE1DF43BDD042F296913C88">
<VendorId>7618CEADE31441C99FE1BC07E325E0FA</VendorId>
<DocTypeId>CE3CD095580647389ED402675B43BE16</DocTypeId>
<AccNo>TXT:</AccNo>
<StmtDate>20161128</StmtDate>
<DDSDocValue name="UniqueDocID" type="text" len="32">J48309A0580113A27E053A2DEF40AC8B8</DDSDocValue>
<NumberOfPages value="8"/>
<Skipped>
<SPages></SPages>
</Skipped>
</document>
<document docID="1" docMasterID="1A5D331DDF8A84385F77B621C3F9CD86" docInstanceID="49E3D61E9CE1DF43BDD042F296913C88">
<VendorId>7618CEADE31441C99FE1BC07E325E0FA</VendorId>
<DocTypeId>CE3CD095580647389ED402675B43BE16</DocTypeId>
<AccNo>TXT:1</AccNo>
<StmtDate>20161128</StmtDate>
<DDSDocValue name="UniqueDocID" type="text" len="32">P48309A0580113A27E053A2DEF40AC8B8</DDSDocValue>
<NumberOfPages value="8"/>
<Skipped>
<SPages></SPages>
</Skipped>
</document>
</eGAD>
What I try to do is, I read the first GUID from text file and compare it with each JRNxml's document group and if I found match means, i'll replace the innertext value with text files second value.
Below is my code
static void Main()
{
//Load the File
using (StreamReader reader = new StreamReader("C:\\temp1\\AllXMLData_FinalOutput.txt"))
{
string line = reader.ReadLine();
string[] value = new string[0];
while ((line = reader.ReadLine()) != null)
{
// Load the file and read the GUID and Plan number one by one From Source Text file
var values = line.Split(',');
string GUIDfromTxt = values[0]; // Holds the GUID value from the Source Txt file
string PlanNofromTxt = values[1]; // Holds the Plan number valuse from the Source Txt file
// Read the XML's one by one from Destination folder and search for the GUID on each group
string folderPath = "C:\\Temp2";
DirectoryInfo di = new DirectoryInfo(folderPath);
FileInfo[] rgFiles = di.GetFiles("*.JRN");
foreach (FileInfo fi in rgFiles)
{
XmlDocument xmljrndoc = new XmlDocument();
XmlNodeList xmlnode;
FileStream fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.ReadWrite); // Open file
xmljrndoc.Load(fs);
xmlnode = xmljrndoc.GetElementsByTagName("document");
string JRNGUID = "";
string JRNPLAN = "";
for (int k = 0; k < xmlnode.Count; k++) // Read all the elements one by one
{
JRNGUID= xmlnode[k].SelectSingleNode("DDSDocValue[#name='UniqueDocID']").InnerText.Trim(); //get guid value from Destination XML
JRNPLAN= xmlnode[k].SelectSingleNode("AccNo").InnerText.Trim(); //get Plan number value from Destination XML
Console.WriteLine("Value From Text File GUID : " + GUIDfromTxt + " Plan Number : " + PlanNofromTxt);
Console.WriteLine("Value From JRN File GUID : " + JRNGUID + " Plan Number : " + JRNPLAN);
if ((GUIDfromTxt == JRNGUID) && (JRNPLAN.Length <= 8)) // check the GUID matches
{
Console.WriteLine("OLD Value : "+ JRNPLAN + " NEW Value : " + PlanNofromTxt); xmlnode[k].SelectSingleNode("AccNo").InnerText.Replace(JRNPLAN, PlanNofromTxt); // replace the txt plan to xml plan tag
}
Console.WriteLine("Xml JRN Value after find and replace " + JRNGUID + " " + JRNPLAN);
}
fs.Close();
//fs.Dispose();
}
Console.ReadKey();
}
//reader.Close();
//reader.Dispose();
}
}
This code is not working in last part means, unable to replace the text in xmlJRN file.
Can some one help me to find the mistake I have done here? you help is much appreciated. I want to replace a value and save files.
EDIT1:
Thanks for your suggestions, I've completed the code. Here is the final one.
static void Main()
{
StreamWriter sw = new StreamWriter("C:\\Temp3\\Log.txt", false, Encoding.ASCII); // log file declaration
string folderPath = "C:\\Temp2";
DirectoryInfo di = new DirectoryInfo(folderPath);
FileInfo[] rgFiles = di.GetFiles("*.JRN");
foreach (FileInfo fi in rgFiles)
{
sw.WriteLine("Opening XML JRN File : " + fi.Name);
XmlDocument xmljrndoc = new XmlDocument();
XmlNodeList xmlnode;
FileStream fs = new FileStream(fi.FullName, FileMode.Open, FileAccess.ReadWrite);
xmljrndoc.Load(fs);
xmlnode = xmljrndoc.GetElementsByTagName("document");
string JRNGUID = "";
string JRNPLAN = "";
for (int k = 0; k < xmlnode.Count; k++) // Read all the elements one by one
{
JRNGUID = xmlnode[k].SelectSingleNode("DDSDocValue[#name='UniqueDocID']").InnerText.Trim(); //get guid value from Destination XML
JRNPLAN = xmlnode[k].SelectSingleNode("AccNo").InnerText.Trim();
sw.WriteLine("FROM XMLJRN file - GUID : " + JRNGUID + " PlanNumber : " + JRNPLAN);
StreamReader reader = new StreamReader("C:\\temp1\\AllXMLData_FinalOutput.txt");
sw.WriteLine("Reading Txt file for GUID Search... ");
string line = reader.ReadLine();
string[] value = new string[0];
while ((line = reader.ReadLine()) != null)
{
// Load the file and read the GUID and Plan number one by one
var values = line.Split(',');
string GUIDfromTxt = values[0]; // Holds the GUID value from the Txt file
string PlanNofromTxt = "Compass:" + values[1]; // Holds the Plan number valuse from the Txt file
sw.WriteLine("FROM text file - GUID : " + GUIDfromTxt + " PlanNumber : " + PlanNofromTxt);
if ((GUIDfromTxt == JRNGUID) && (JRNPLAN.Length <= 8)) // check the GUID matches
{
sw.WriteLine("GUID MATCH FOUND!");
sw.WriteLine("OLD Value : " + JRNPLAN + " replaced with NEW Value : " + PlanNofromTxt);
fs.Close();
FileStream fs1 = new FileStream(fi.FullName, FileMode.Append, FileAccess.Write);
xmljrndoc.Save(#"C:\\Temp3\\" + fi.Name); // replace the txt plan to xml plan tag
fs1.Close();
// xmljrndoc.Save(fi.FullName);
}
else
{
sw.WriteLine("GUID MATCH NOT FOUND!");
}
}
}
}
sw.Close();
}
String.Replace does not actually modify the string directly. It returns a copy of the string with the replacement. You still need to assign the modified value to InnerText yourself.
For example, instead of:
xmlnode[k].SelectSingleNode("AccNo").InnerText.Replace(JRNPLAN, PlanNofromTxt);
you need to do something like:
var node = xmlnode[k].SelectSingleNode("AccNo");
node.InnerText = node.InnerText.Replace(JRNPLAN, PlanNofromTxt);
I have taken the input code through file and i have to generate data according to it and output it's result in a text file as well..
My Output Code is below..
public void Generator()
{
/// ....... Code
public void DisplayTOKENS()
{
using (StreamWriter writer =
new StreamWriter("C:\\Users\\saeed\\Documents\\Outputt.txt"))
{
for (int i = 0; i < j;i++ )
{
tok[i].Display_Token();
} }
}
// and in other structur named TOKEN
public void Display_Token()
{ /*
using (StreamWriter writer =
new StreamWriter("C:\\Users\\saeed\\Documents\\Outputt.txt"))
{
writer.Write("( " + this.Class_Part + " , ");
writer.Write(this.Value_Part + " , ");
writer.Write(this.Line_no + " )");
writer.WriteLine();
}*/
Console.Write("( " + this.Class_Part + " , ");
Console.Write(this.Value_Part + " , ");
Console.Write(this.Line_no + " )");
Console.WriteLine();
}
When i try to directly work in Display_Token then it just simply show the last line in file.. i want to display the complete array in the file. waiting for some positive response !!
That StreamWriter constructor overwrites the existing file. So, each token effectively deletes whatever was written earlier then writes its content. That is why you only see the last token's content in the file.
Use the overload with the "append" argument and pass true so that the existing file is not deleted.
You have to check if file exists and than do "append" operation instead of "overwrite".
// in DisplayTOKENS()
string fileName = "C:\\Users\\saeed\\Documents\\Outputt.txt";
if (System.IO.File.Exists(fileName))
System.IO.File.Delete(fileName);
for (int i = 0; i < j; i++)
{
tok[i].Display_Token(fileName);
}
// in Display_Token(string fileName)
System.IO.File.AppendAllText(fileName, "( " + this.Class_Part + " , " + this.Value_Part + " , " + this.Line_no + " )");
I am a bit in a pickle regarding a consolidation application we are using in our company. We create a csv file from an progress database this csv file has 14 columns and NO header.
The CSV file contains payments (around 173 thousand rows). Most of these rows are the same except for the column amount (last column)
Example:
2014;MONTH;;SC;10110;;;;;;;;EUR;-6500000
2014;01;;SC;10110;;;;;;;;EUR;-1010665
2014;01;;LLC;11110;;;;;;;;EUR;-6567000
2014;01;;SC;10110;;;;;;;;EUR;-1110665
2014;01;;LLC;11110;;;;;;;;EUR;65670.00
2014;01;;SC;10110;;;;;;;;EUR;-11146.65
(around 174000 rows)
As you can see some of these lines are the same except for the amount column. What i need is to sort all rows, add up the amount and save one unique row instead of 1100 rows with different amounts.
My coding skills are failing me to get the job done within a certain timeframe, maybe one of you can push me in the right direction solving this problem.
Example code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string input = File.ReadAllText(#"c:\temp\test.txt");
string inputLine = "";
StringReader reader = new StringReader(input);
List<List<string>> data = new List<List<string>>();
while ((inputLine = reader.ReadLine()) != null)
{
if (inputLine.Trim().Length > 0)
{
string[] inputArray = inputLine.Split(new char[] { ';' });
data.Add(inputArray.ToList());
}
}
//sort data by every column
for (int sortCol = data[0].Count() - 1; sortCol >= 0; sortCol--)
{
data.OrderBy(x => x[sortCol]);
}
//delete duplicate rows
for (int rowCount = data.Count - 1; rowCount >= 1; rowCount--)
{
Boolean match = true;
for (int colCount = 0; colCount < data[rowCount].Count - 2; colCount++)
{
if(data[rowCount][colCount] != data[rowCount - 1][colCount])
{
match = false;
break;
}
}
if (match == true)
{
decimal previousValue = decimal.Parse(data[rowCount - 1][data[rowCount].Count - 1]);
decimal currentValue = decimal.Parse(data[rowCount][data[rowCount].Count - 1]);
string newStrValue = (previousValue + currentValue).ToString();
data[rowCount - 1][data[rowCount].Count - 1] = newStrValue;
data.RemoveAt(rowCount);
}
}
string output = string.Join("\r\n",data.AsEnumerable()
.Select(x => string.Join(";",x.Select(y => y).ToArray())).ToArray());
File.WriteAllText(#"c:\temp\test1.txt",output);
}
}
}
Read the CSV file line by line, and build an in-memory dictionary in which you keep the totals (and other information you require). As most of the lines belong to the same key, it will probably not cause out of memory issues. Afterwards, generate a new CSV based on the information in the dictionary.
As I interpret your question, your problem and the solution you are asking for are how to take your input that are in the form of
#"2014;MONTH;;SC;10110;;;;;;;;EUR;-6500000
2014;01;;SC;10110;;;;;;;;EUR;-1010665
2014;01;;LLC;11110;;;;;;;;EUR;-6567000
2014;01;;SC;10110;;;;;;;;EUR;-1110665
2014;01;;LLC;11110;;;;;;;;EUR;65670.00
2014;01;;SC;10110;;;;;;;;EUR;-11146.65"
Get the last column and then sum it up? If so this is actually very easy to do with something like this
public static void Main()
{
string input = #"2014;MONTH;;SC;10110;;;;;;;;EUR;-6500000
2014;01;;SC;10110;;;;;;;;EUR;-1010665
2014;01;;LLC;11110;;;;;;;;EUR;-6567000
2014;01;;SC;10110;;;;;;;;EUR;-1110665
2014;01;;LLC;11110;;;;;;;;EUR;65670.00
2014;01;;SC;10110;;;;;;;;EUR;-11146.65";
var rows = input.Split('\n');
decimal totalValue = 0m;
foreach(var row in rows)
{
var transaction = row.Substring(row.LastIndexOf(';') +1);
decimal val = 0m;
if(decimal.TryParse(transaction, out val))
totalValue += val;
}
Console.WriteLine(totalValue);
}
But maybe I have misunderstood what you were asking for?
Sorry answering my post so late but this is my final solution
Replacing all " characters and write the output to the stream writer. (going from 25mb to a 15mb file.). Than copy my CSV file to the SQL server so i can bulk insert. After my insert i just query the table and read / write the result set to a new file. My new file is only +/-700KB!
The Filldata() method is filling a datagridview in my application so you can review the result instead of opening the file in excel.
I am new with C#, i am currently writing a new solution to query the csv file directly or in memory and write it back to a new file.
Method1:
string line;
StreamWriter sw = new StreamWriter(insertFile);
using (StreamReader sr = new StreamReader(sourcePath))
{
while ((line = sr.ReadLine()) != null)
{
sw.WriteLine(line.Replace("\"", ""));
}
sr.Close();
sw.Close();
sr.Dispose();
sw.Dispose();
File.Copy(insertFile, #"\\SQLSERVER\C$\insert.csv");
}
Method2:
var destinationFile = #"c:\insert.csv";
var querieImportCSV = "BULK INSERT dbo.TABLE FROM '" + destinationFile + "' WITH ( FIELDTERMINATOR = ';', ROWTERMINATOR = '\n', FIRSTROW = 1)";
var truncate = #"TRUNCATE TABLE dbo.TABLE";
string queryResult =
#"SELECT [Year]
,[Month]
,[Week]
,[Entity]
,[Account]
,[C11]
,[C12]
,[C21]
,[C22]
,[C3]
,[C4]
,[CTP]
,[VALUTA]
,SUM(AMOUNT) as AMOUNT
,[CURRENCY_ORIG]
,[AMOUNTEXCH]
,[AGENTCODE]
FROM dbo.TABLE
GROUP BY YEAR, MONTH, WEEK, Entity, Account, C11, C12, C21, C22, C3, C4, CTP, VALUTA, CURRENCY_ORIG, AMOUNTEXCH, AGENTCODE
ORDER BY Account";
var conn = new SqlConnection(connectionString);
conn.Open();
SqlCommand commandTruncate = new SqlCommand(truncate, conn);
commandTruncate.ExecuteNonQuery();
SqlCommand commandInsert = new SqlCommand(querieImportCSV, conn);
SqlDataReader readerInsert = commandInsert.ExecuteReader();
readerInsert.Close();
FillData();
SqlCommand commandResult = new SqlCommand(queryResult, conn);
SqlDataReader readerResult = commandResult.ExecuteReader();
StringBuilder sb = new StringBuilder();
while (readerResult.Read())
{
sb.Append(readerResult["Year"] + ";" + readerResult["Month"] + ";" + readerResult["Week"] + ";" + readerResult["Entity"] + ";" + readerResult["Account"] + ";" +
readerResult["C11"] + ";" + readerResult["C12"] + ";" + readerResult["C21"] + ";" + readerResult["C22"] + ";" + readerResult["C3"] + ";" + readerResult["C4"] + ";" +
readerResult["CTP"] + ";" + readerResult["Valuta"] + ";" + readerResult["Amount"] + ";" + readerResult["CURRENCY_ORIG"] + ";" + readerResult["AMOUNTEXCH"] + ";" + readerResult["AGENTCODE"]);
}
sb.Replace("\"","");
StreamWriter sw = new StreamWriter(homedrive);
sw.WriteLine(sb);
readerResult.Close();
conn.Close();
sw.Close();
sw.Dispose();
So I want to put certain lines into a text box, say I use the "Search Function" to search transaction ID, it would look through the transactions.txt file and find the transaction ID and Read the 6 lines under it which show the transactions Details, once found this would then go to a the listbox which then you could edit the transaction.
I was wondering would you use loops and arrays to do this, and could someone show me how, Thank you!
Heres my current code:
//Creates a textfile with details of the transaction
public void CreateFile()
{
StreamWriter outputFile;
outputFile = File.AppendText("Transactions.txt");
outputFile.WriteLine("Investor :" +" " + InvestorNameLabel.Text);
outputFile.WriteLine("Initial Amount" + " " +AmountLabel.Text);
outputFile.WriteLine("Date Invested" +" " +DateLabel.Text);
outputFile.WriteLine("Period Chosen" + " "+DaysInvestedLabel.Text);
outputFile.WriteLine("Rate Chosen" + " " + RateLabel.Text);
outputFile.WriteLine("Total Interest" + " " +InterestAmountLabel.Text);
outputFile.WriteLine("Transaction Number :" + " " + TransactionIDLabel.Text);
outputFile.Close();
MessageBox.Show("Transaction file for Transaction: " + TransactionIDLabel.Text + " " +"Was Created", "Transaction File");
}
//puts all transactions in listbox
//needs to be able to find certain transactions
private void button1_Click(object sender, EventArgs e)
{
using (StreamReader sr = new StreamReader("transactions.txt"))
{
string line;
while ((line = sr.ReadLine()) != null)
{
listBox1.Items.Add(line);
}
}
}
Try This:
string ID = "23";
bool idFound=false;
int count = 0;
foreach (var line in File.ReadLines("transactions.txt"))
{
if (idFound && count < 6)
{
listBox1.Items.Add(line);
count++;
}
if(line.Contains(ID))//if you wantto match exactly use if(line.Equals(ID))
{
idFound = true;
}
}
I have been doing this for a university project and have run into a problem. I have managed to load multiple lines from a file but I am unable to save them back to a file. I can save a single string to the file, which is the last string processed but that is it. I may be doing it completely wrong by performing a loop, but I cant think of any other way to do it. The coding for the savefile section is as follows:
case "s":
case "8":
{
int savecount = 0;
string savestring = "";
//Clear the text file ready to be saved
using (FileStream fs = File.Create("billing.txt"))
{
}
while (savecount != CustomerCount)
{
using (StreamWriter save = new StreamWriter("billing.txt"))
{
//Create the string to save to the file
savestring = CustomerNumber[savecount] + ","
+ CustomerName[savecount] + ","
+ Address[savecount] + ","
+ RateScheme[savecount] + ","
+ PeakKWH[savecount] + ","
+ OffPeakKWH[savecount] + ","
+ StandardKWH[savecount];
Console.WriteLine(savestring);
save.WriteLine(savestring);
savecount++;
Console.ReadLine();
}
}
Console.WriteLine("All data saved successfully");
Console.ReadLine();
break;
}
Not sure where to go from here. Any help would be appreciated
You should open the file for saving before the loop. E.g.
using (StreamWriter save = new StreamWriter("billing.txt")) {
while (savecount != CustomerCount) {
// rest of your code here
At the moment, you are opening the file in each loop, writing a line out. Then re-opening it (and losing the data already written).
As pointed out in the comments, you don't need to call File.Create. By default the StreamWriter will overwrite the existing file.
You need the while loop inside the using { } As it is you're overwriting your data each time, leaving the last item in your file when you look at it:
using (StreamWriter save = new StreamWriter("billing.txt"))
{
while (savecount != CustomerCount)
{
//Create the string to save to the file
string savestring = CustomerNumber[savecount] + ","
+ CustomerName[savecount] + ","
+ Address[savecount] + ","
+ RateScheme[savecount] + ","
+ PeakKWH[savecount] + ","
+ OffPeakKWH[savecount] + ","
+ StandardKWH[savecount];
Console.WriteLine(savestring);
save.WriteLine(savestring);
savecount++;
Console.ReadLine();
}
}
What You are doing wrong is, you are opening the file in each iteration of while, writing a line in file and Then again re-opening the file and overwriting the contents. You can rechange your code
using (StreamWriter save = new StreamWriter("billing.txt"))
{
while (savecount != CustomerCount)
{
// rest of string formation of saveString logic and save.WriteLine(savestring); goes here
.....
}
}
I think you can use a simple code also where you can save all your input string in an List and use File.WriteAllLines function as
{
....
List<string> Customers = new List<string>();
for (savecount = 0; savecount < CustomerCount; savecount++)
{
//Create the string to save to the file
Customers.Add( CustomerNumber[savecount] + "," + CustomerName[savecount] + "," + Address[savecount] + "," + RateScheme[savecount] + "," + PeakKWH[savecount] + "," + OffPeakKWH[savecount] + "," + StandardKWH[savecount]);
Console.WriteLine(Customers[savecount]);
}
string filePath = "billing.txt"; // This is your file path where all the contents are to be written
File.WriteAllLines(filePath, Customers);
..........
}
You need:
using (StreamWriter save = new StreamWriter("billing.txt")) {
while (savecount != CustomerCount) {
You have to open file before loop because opening inside deletes all previous data written in that, also it takes some time for opening.
However you can open file inside loop, but you need to set append file, it would be:
StreamWriter save = new StreamWriter("billing.txt", true)
This option is slower and you may need to clear file before opening in append mode, so it isn't the best option.