file is not read by completely [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I have an file text with approximate 113687 lines, but my application reads only 314 lines, can anyone say why?
My code:
string file = #"z:\foo.txt";
StreamReader reader = File.OpenText(file);
string line;
int rows = 0;
while ((line = reader.ReadLine()) != null) {
++rows;
doSomethingWith(line);
// ...
}
The DoSomethingWith function is similar to:
protected static bool DoSomethingWith(string line)
{
return Regex.Match(line, #"\d+\-\d+\.\d+\.\d+\.\d+").Success;
}
Updated:
In answer to Gregs question:
Does your foo.txt contain a Ctrl+Z character on line 314?
Yes, my file contains a Control-Z character on line 314.

Text files on Windows can be terminated with a Ctrl+Z character. This means that when the file is read, the StreamReader returns end-of-file when the Ctrl+Z is encountered. Any data following the Ctrl+Z is not read.
If you wish to read the entire file without this text-mode behaviour, use File.OpenRead instead of File.OpenText.

Related

How to get No extension file type [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want get file type but my file noting extension in C#,Asp.net.
I want get file type from downloaded file with webClient.DownloadFile.
Can trace file?
Can understand downloaded file is pdf or no?
There is no absolute way to get the file type of a file with no extension.
Some file types, such as EXE, have a header that you could check. Other files, such as text files, could probably be detecting using some sort of heuristic that looks for only text characters. But still other files would not be identifiable.
As long as you know the extension beforehand it's possible, otherwise you'd have to be familiar with the file's structure. If all you're doing is downloading a pdf without the .pdf extension, rename your file [something].pdf when you download it and you'll be able to access it as a regular pdf.
There are some links where same question has been asked . I hope it helps
https://superuser.com/questions/435224/how-do-i-find-out-the-file-type-without-an-extension
http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/16c34850-8799-4a8b-8702-2f59d96e79c1
http://www.troublefixers.com/open-files-without-any-valid-file-extension/
http://bytes.com/topic/c-sharp/answers/812692-open-file-without-knowing-extension
FileStream fs = new FileStream(#"c:\a.pdf", FileMode.Open, FileAccess.Read);
StreamReader r = new StreamReader(fs);
string pdfText = r.ReadToEnd();
Regex rx1 = new Regex(#"/Type\s*/Page[^s]");
MatchCollection matches = rx1.Matches(pdfText);
MessageBox.Show("The PDF file has " + matches.Count.ToString() + " page(s).";

Search in a text file from one symbol to another [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am coding program, and stacked. Please can give me a code which search text in file from one specific symbol to another using C# visual Windows Forms , not console application. Like this text in textfile c:\id.txt
The entry was successfully copied to {ea4c4653-cc65-11e1-a2fc-001e101f4e71}.
search string from { to } , and result with { and }, without . at the end. And send found text in a message box.Code to search text in a file an send whole line in message box. But i need part of line.
Regex can be useful:
MessageBox.Show(
Regex.Match(inputString, "\{(?<path>[^}]*)\}").Groups["path"].Value);
explain:
{ '{'
[^}]* any character except: '}'
(0 or more times, matching the most amount possible)
} '}'
Try by using regular expressions:
var line = " The entry was successfully copied to {ea4c4653-cc65-11e1-a2fc-001e101f4e71}.";
var foo = Regex.Match(line, #"to\s*\{([^}]+)\}");
if(foo.Success) {
MessageBox.Show(foo.Groups[1].Value); //ea4c4653-cc65-11e1-a2fc-001e101f4e71
} else {
//not found value
}

The C# equivalent [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I am trying to close the program :HHTCntrl.exe as if the user clicked exit on that program. I have the following Borland C++ code. What is the C# equivalent?
FILE *sf;
AnsiString ClosePollControl = AnsiString("HHT")+cbHHTNo->Text+AnsiString(".cls");
sf = fopen(ClosePollControl.c_str(),"w");
fclose(sf);
You're opening the file for writing. Assuming that cbHHTNo is the string containing file name, in C# it will go like that:
var path = "HHT" + cbHHTNo + ".cls";
using (var file = File.OpenWrite(path))
{
// do sth with the open file stream here
}
Well, it is actually pretty simple.
There isn't a concept of an AnsiString in C# so you have to use a string.
string ClosePollControl = "HHT" + cbHHTNo->Text + ".cls";
// Open the stream and write to it.
using (FileStream fs = File.OpenWrite(ClosePollControl))
{
}
This assumes you have some user level control, cbHHTNo, that you are getting the Text (e.g. string).
If you are trying to shutdown the process then use System.Diagnostics.Process class. For example,
Process process = Process.GetProcessesByName("HHTCntrl.exe").FirstOrDefault();
if (process != null)
{
process.Kill(); // to immediatelly kill the process or use process.CloseMainWindow() to gracefully "kill" the process
}
If you want to close an active form use Close() method.

What is the most robust way to open file and read content [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I usually do this when I have to read the whole file always:
using (var fileStream = File.OpenRead(...))
using (var reader = new StreamReader(fileStream))
var content = reader.ReadToEnd();
Any better/faster way?
string content = System.IO.File.ReadAllText(#"your file path");
If you want to get lines:
string[] lines = System.IO.File.ReadAllLines(#"your file path");
if you want to only read lines until get to some line then use the IEnumerable ReadLines:
foreach(string line in System.IO.File.ReadLines(#"your file path")
{
if (line == ...)
{
break;
}
}

How do I remove a portion of my string? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Continuing from my previous question, I now want to remove the number once I have found it and stored it in a variable.
Just a slight tweak to my response to your first question to instead use Regex.Replace method will git 'er done.
Don't worry I figured it out. Just need to find the length then delete the chars
(qual is the intergers found)
string length = qual.ToString();
int length2 = length.Length;
text.Remove(0, length2);
I think the following code resolves the root problem:
string originalString = "35|http://www.google.com|123";
string[] elements = originalString.Split(new char[] { '|' }, 2);
int theNumber = int.Parse(elements[0]); // 35
string theUrl = elements[1]; // http://www.google.com|123

Categories