Removing characters from a file
Author |
Message |
SwAnK
|
Posted: Wed Feb 15, 2006 11:58 pm Post subject: Removing characters from a file |
|
|
hello, what im trying to do is import a file i made with a bunch of words and numbers and commas, and then remove all the commas just leaving the numbers and words. When it does run it comes up with the error "string coerced to char is not length 1." heres the code, could anyone help me out? thanx.
code: | var fileName : string := "data.txt"
var fileNo : int
var thefile : char
var file_length : string
var mogo : string
open : fileNo, fileName, read
if fileNo < 0 then
put "no file found!!"
end if
read : fileNo, file_length
close : fileNo
open : fileNo, fileName, get
for i : 1 .. length (file_length)
get : fileNo, thefile
if file_length (i) = chr (44) or file_length (i) = " " then
thefile := file_length (1 .. i - 1)
end if
end for
put thefile .. |
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Cervantes
![](http://compsci.ca/v3/uploads/user_avatars/1023105758475ab2e040bde.jpg)
|
Posted: Thu Feb 16, 2006 8:03 am Post subject: (No subject) |
|
|
You're doing a lot of things wrong, here.
- "thefile" is a bad name for a char variable. It signifies that it is an integer representing a file stream, not a character.
- file_length implies that this variable stores the length of the file. That would be an integer, possibly representing the number of bytes or the number of lines. It does not make sense to have file_length a string.
- What is mogo?
- read is for random access. Usually it is used only to read something that has been writed (yes, I know "writed" is not a word). Reading the file (assuming it is a standard txt or csv file) will not act as you expect. Use get.
- Your for loop probably has a messed up range, because file_length probably contains a bunch of jibberish.
- You're trying to get "thefile" from fileNo. "thefile" is a char (again, you wouldn't think it was a char judging by the variable name). When you use get, it gets one word at a time. So, as soon as you've got a word with more than one character in your file, your program will crash on that line, with the error message you gave. "thefile" should be renamed to "input_word" and made a string.
- The if statement makes no sense. You're trying to use "file_length" (which you think contains all the data in the file, but doesn't) to find a comma or a null string, I believe. If it is, then "thefile" is assigned the value of "file_length", minus the last character. Actually, the error might come from this line first: you're trying to assign a multi-character string to a char data type. Nu uh.
Check out the Turing Walkthrough to get links to tutorials on file input/output and string manipulation. |
|
|
|
|
![](images/spacer.gif) |
Clayton
![](http://compsci.ca/v3/uploads/user_avatars/1718239683472e5c8d7e617.jpg)
|
Posted: Thu Feb 16, 2006 9:19 am Post subject: (No subject) |
|
|
thats some uh, innovative code you've got there, if you want to find all commas and remove them then make an array with the upper bounds equal to the length of your information, then, make a for loop with the upper bounds being the upper bounds of your array. then code something like this
code: |
for x: 1..upper(stringArray)
%assume we've made this array and made all elements in the array initialized
if stringArray(x)="," then
stringArray(x):=" "
end if
end for
|
so what we've done here is created a loop to find all commas and replace them with spaces if you're wondering how this all got into an array it would look something like this
code: |
%assume we've already got the data from file
var stringArray:array 1..length(fileData) of string
for x:1..upper(stringArray)
stringArray(x):=fileData(x)
end for
|
this is taking the string data (fileData) and taking it apart character by character and putting it into stringArray and then you would go into your loop to get rid of commas hope this helps and you really should go to the Turing Walkthrough |
|
|
|
|
![](images/spacer.gif) |
MysticVegeta
![](http://www.geocities.com/ohsoinsane/my_avatar.JPG)
|
Posted: Thu Feb 16, 2006 11:11 am Post subject: (No subject) |
|
|
Why not just search around for a module called String.Replace that I posted... could replace any char with any. |
|
|
|
|
![](images/spacer.gif) |
|
|