|
label_read:
b_beforeRead = line.Contains(s_begin);
line = sr.ReadLine();
b_afterRead = line.Contains(s_end);
In the above code, if ReadLine hits the end of the input file then line will be null . But this code is so badly constructed it needs a complete rework.
|
|
|
|
|
...which JSOP wrote for him and he won't use it...
I smell a help vampire in the making.
|
|
|
|
|
Dave Kreskowiak wrote: help vampire My tech vocabulary just grew.
Software Zen: delete this;
|
|
|
|
|
We've had a few of them over the years.
|
|
|
|
|
Yes, I tested your code and saw the error. I do not think writing from one text file to another should require the volume of codes I saw, I may be wrong, even when you need to filter some lines.
If I know precisely what you want to do, I can help you, I did something similar before.
You may give detailed explanation of what you want to achieve.
|
|
|
|
|
//Try this procedure and manipulate it
public void RewriteFile(string SourceFileFullName, string TargetFileFullName, string UnWantedFilterString = "")
{
//UnWantedFilterString variable allows a line to be eliminated in the target file if it is contained in the line, this can be fine-tuned depending on your need.
//SourceFileFullName and TargetFileFullName should be full file path and name
if (!File.Exists(SourceFileFullName))
{
MessageBox.Show("Source File Does Not Exist, Confirm on the Disk.");
return;
}
string CurrentLine = "";
string CurrentLineTrimedUcase = "";
string UnWantedFilterUpperTrimed = UnWantedFilterString.Trim().ToUpper(); //This is string of anything you do not want to eliminate a line
try
{
StringBuilder sb2 = new StringBuilder("");
using (StreamReader sr = File.OpenText(SourceFileFullName))
{
while ((CurrentLine = sr.ReadLine()) != null)
{
CurrentLineTrimedUcase = CurrentLine.Trim().ToUpper().Replace("\t", ""); //Avoid tabs, trim and change to upper case
if (UnWantedFilterUpperTrimed == "") //Test for unwated Filter string
{
sb2.AppendLine(CurrentLine); // Append line string to StringBuilder if unwanted filter empty
}
else
{
if (CurrentLineTrimedUcase.IndexOf(UnWantedFilterUpperTrimed) < 0)
{
sb2.AppendLine(CurrentLine); // Only Append line string to StringBuilder if unwanted filter not in string
}
}
}
}
if (sb2.Length == 0)//Empty file will not be written
{
MessageBox.Show("Empty File will not be re-written");
return;
}
//Delete target file if it exists
if (File.Exists(TargetFileFullName))
{
File.Delete(TargetFileFullName);
}
//Write file now
using (StreamWriter sw = File.CreateText(TargetFileFullName))
{
sw.Write(sb2);
}
MessageBox.Show("File Written Successfully");
return;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message + " --- File Not Written");
}
}
|
|
|
|
|
I'm trying to write an app that allowed me to copy data from an Android app to a C# app via USB. I will probably write the data to a json file and copy the file to my C# app, either by sending it from Android or copying it from C#.
I found this api but it looks old and I can't find any documentation.
Does anyone know how I can send or copy a file from Android app to a C# app via USB?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
modified 31-Mar-22 1:02am.
|
|
|
|
|
If you connect the Android device to a PC via USB then it should appear as a new disk drive. A quick test with your device should confirm that and you can then see whether you can access the files.
|
|
|
|
|
I thought so too, but it doesn't show up. And I have Developer Mode and USB Debugging turned on
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
That has always worked for me, but see also @OriginalGriff's comments below.
|
|
|
|
|
Richard is right, but ... it may depend on how the Android device is configured
.
There are two ways Android can connect to a PC - MTP for media transfer (pictures and videos), or UMS (for files): Android USB Connections Explained: MTP, PTP, and USB Mass Storage[^]
If the device is selected to "Charge only" or MTP, you won't see the device as a file source.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Thanks. I'll look into it
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
I got it now. I went into Settings => USB Preferences and selected "File Transfer". Now I can see the tablet and all the folders under it.
Thanks for pointing me in the right direction.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
You're welcome!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi all,
I am receiving errors when executing below code.
The code asks me how many items the array has, and then let me fill the array with input.
The problem is whenever I input letters instead of numers I get an exception.
System.FormatException: 'Input string was not in a correct format.
The error happens at the following lines
int size = Convert.ToInt32(Console.ReadLine());
&
array[i] = Convert.ToInt32(Console.ReadLine());
Now I know that I might be using the int.TryParse method, by I do not know how to implement it.
Of perhaps when the input is not valid, give an error like
Console.Write("Wrong input, only numbers");
My code:
Console.Write("How many items in the array ");
int size = Convert.ToInt32(Console.ReadLine());
int[] array = new int[size];
Console.WriteLine("Enter {0} array items", size);
for (int i = 0; i < size; i++)
{
array[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("\nThe following items are in the array: ");
for (int i = 0; i < size; i++)
{
Console.Write("{0} ", array[i]);
}
Console.ReadLine();
|
|
|
|
|
Yeah, you cannot convert strings with letters to an integer since there is no logical conversion.
The documentation on Integer.TryParse can be found here[^] complete with examples and you can even run the example code in the browser and play around with it.
|
|
|
|
|
All fine. Thanks.
modified 26-Mar-22 18:18pm.
|
|
|
|
|
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.
So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
So how to generate 5 random numbers that fall into the range between 0 and 10000 and assign them to an array? Thanks.
|
|
|
|
|
Think about it: how do you generate one random number? Hint: see the Random class ...
Get that working, and you can then generate as many as you need.
Get that working, and you can store them in an array.
Get that working, and you can start on the rest of the task.
As the link I originally gave you explains: take a "difficult task" and break it into smaller ones. The smaller ones are "doable" on their own, and doing all of those means you do the "impossible" task as well!
Read the article, and see how far you can get.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I made the initial code as below:
using System;
public class Program
{
public static void Main(string[] args)
{
int N = 10;
int[] array = new int[N];
Random rnd = new Random();
for (int i = 0; i < N; i++)
{
array[i] = rnd.Next(10000);
}
for (int j = 0; j < N; j++)
{
Console.Write(array[j])
}
}
}
But it fails to display any code. Can you please help? Thanks.
|
|
|
|
|
Chances are it does, but it closes down too fast and you can't see it ...
Run it in the debugger, and put a breakpoint on the close curly bracket at the end of the Main method. When you run it, it will stop at the breakpoint and you can see what has happened (or run it again and step through to see what it does while it is running!)
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
It works now. The semicolon was missing.
|
|
|
|
|
If you get an error, it helps if you tell us that, than just assuming we will have seen that on your screen!
You will be solving syntax errors often - we all do - so this may help you get started: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^]
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Please do not delete your original question, as the full thread could be helpful to other people. Just add "[Solved]" to the message's title.
|
|
|
|