• We should now be fully online following an overnight outage. Apologies for any inconvenience, we do not expect there to be any further issues.

thread can now be deleted, thanks mods

EyeMWing

Banned
Jun 13, 2003
15,670
1
0
have it search for 10, 13 or 10 AND 13 (A lot of the time they will appear together, but this will only create ONE line break)
 

Howard

Lifer
Oct 14, 1999
47,982
11
81
Originally posted by: EyeMWing
have it search for 10, 13 or 10 AND 13 (A lot of the time they will appear together, but this will only create ONE line break)
Thanks, I guess I was on the right track.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Originally posted by: EyeMWing
have it search for 10, 13 or 10 AND 13 (A lot of the time they will appear together, but this will only create ONE line break)
Yes, DOS/Windows use the pair, c++ "\r\n", but unix/linux/mac files often only include the "\n".

For the final line of a file, sometimes there is a \n or \r\n and sometimes there is not.
 

Howard

Lifer
Oct 14, 1999
47,982
11
81
New question. How do I delete a line from a text file? I've tried writing all the lines EXCEPT the one I want deleted to a temporary file, then writing the lines of the temporary file into the original file, thus removing the line to be deleted. But it's not working... here's my code for that:

public static void delcountry (String inputfile) throws IOException
{
c.println ();
String line, country;
c.println ("Please enter the country you would like deleted from the file. ");
country = c.readLine ();
BufferedReader input;
PrintWriter output;
input = new BufferedReader (new FileReader (inputfile));
output = new PrintWriter (new FileWriter ("temp.txt", true));
line = input.readLine ();
while (line != null)
{
if (line != country)
{
output.println (line);
line = input.readLine ();
}
}
input.close ();
output.close ();
input = new BufferedReader (new FileReader ("temp.txt"));
output = new PrintWriter (new FileWriter (inputfile));
line = input.readLine ();
while (line != null)
{
output.println (line);
line = input.readLine ();
}
input.close ();
output.close ();
}

If you see a "c" as in c.println();, it means something like "System.out"
 

EyeMWing

Banned
Jun 13, 2003
15,670
1
0
I don't think you're closing your temporary file before you try to copy it over to the original file. But your textfileIO class is a great deal different than mine, so I can't be sure that's what the problem is.
 

Howard

Lifer
Oct 14, 1999
47,982
11
81
So does "if (line != country)" work to exclude the line if the contents of the line matches the country to be deleted?

The text file is a plain list of countries.
 

crystal

Platinum Member
Nov 5, 1999
2,424
0
76
is this compare "if (line != country)" work? i.e. try this code: if (line == country) to see if it will only print out the country you enter. BTW, I thought "==" or "!=", etc is only good on number.
 

EyeMWing

Banned
Jun 13, 2003
15,670
1
0
Originally posted by: Howard
So does "if (line != country)" work to exclude the line if the contents of the line matches the country to be deleted?

The text file is a plain list of countries.

Ahh. No it doesn't. It reads in the line as a string, and country is a string.

Do

if (line.equals(country)==false)

String.equals(String) is a built in String method. Just doing a flat out != comparison will check the memory addresses, because a String is not a primitive data type.