Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating a file name based on user input
#1
ok...I appreciate everyone's response to my lasy posting...and that would have worked, except it turns out it's slightly more complicated than I made it out to be. Here's what I have tried, followed by what I actually need the code to accomplish:

INPUT “Enter the device serial number: “, serialnum
LET DEVICE$ = serialnum
OPEN "DEVICE$" FOR INPUT AS #1

What I am TRYING to acccomplish, with absolutely no success, is to prompt the user to enter a device serial number, which becomes the filename of the file I want to create and write data to. So let's say I have 40 samples and I'm currrently looking at number 37. I prompt the user to enter the device serial number, they enter 37 and a file whose name is 37 is created and opened for data entry. IS this possible?
Reply
#2
Code:
INPUT “Enter the device serial number: “, serialnum$
DEVICE$ = serialnum$
OPEN DEVICE$ FOR INPUT AS #1

#1, you can't set a string as a number like that. Incompatible types.
#2, LET isn't needed
#3, you had ""s that means whatever was in it was a litereal string. You are trying to open a string named DEVICE$

Now if you still want to input a number instead of a string (that contains numbers)

Code:
INPUT “Enter the device serial number: “, serialnum
DEVICE$ = ltrim$(str$(serialnum))
OPEN DEVICE$ FOR INPUT AS #1

#1, str$, make the number a string. It creates a leading whitespace.
#2, ltrim$, clear off the leading whitespace

or you can do it this way.
(untested from memory)
Code:
DIM DEVICE AS STRING, serialnum as string
INPUT “Enter the device serial number: “, serialnum
serialnum=ltrim$(str$(val(serialnum)))
DEVICE = serialnum
OPEN DEVICE FOR INPUT AS #1
there heart of it is
Code:
serialnum=ltrim$(str$(val(serialnum)))
It makes what you input a number with 'val()' if it contained something other then a number then it returns 0
i told you what the other functions do. =P

this makes it so whatever you enter will always be a number (or at least 0).
you can loop it around to accept any number above 0.

Code:
DIM DEVICE AS STRING, serialnum as string
DO
INPUT “Enter the device serial number: “, serialnum
serialnum=ltrim$(str$(val(serialnum)))
LOOP until val(serialnum)>0
DEVICE = serialnum
OPEN DEVICE FOR INPUT AS #1


That's probably very confusing. >_>just ask questions.
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#3
Hmm... A code tag wold have been nice... but its a small amont of code and I still can't edit...

But.. I noticed three mistakes in your code:

1) When you open a file with a string, you do not need quotations:

[syntax="Qbasic"]'Where you have this:

OPEN "DEVICE$" ....

'It needs to be:

OPEN DEVICE$ ....[/syntax]

2) Another thing is to write and create a file, you need to OUTPUT not INPUT... Where as you might think INPUT to file, and OUTPUT from file,.. thats incorrect,.. its ment as: INPUT from file to program, and OUTPUT from program to file.. thus it needs to be:

[syntax="Qbasic"]OPEN DEVICE$ FOR OUTPUT AS #1[/syntax]

3) To go from Number to String you need to use the SRT$() statement, or when you use INPUT, use a string:

[syntax="Qbasic"]INPUT "Enter ...", num
DEVICE$ = STR$(num)

'or

INPUT "Enter ...", num$
DEVICE$ = num$

'or, the most easy way:

INPUT "Enter ...", DEVICE$[/syntax]

The whole DEVICE$ = num can be avoided if you see the last example, and the statment LET doens't need to be there.....

As for it working, I'm not sure why it wouldn't.... Experament using these tips and see if it gives you the right effects.. :wink:

Edit: Darn.. I type to slow...
Kevin (x.t.r.GRAPHICS)

[Image: 11895-r.png]
Reply
#4
hehe. yeah but I missed the OUTPUT thing.
[Image: sig.php]
Back by popular demand!
I will byte and nibble you bit by bit until nothing remains but crumbs.
Reply
#5
No matter which one I try, I keep getting "File not found"

HELP!!!
Reply
#6
If you use INPUT, then it will return that error... OUTPUT, if there is no file, then it will creat it.. are you sure you are using OUTPUT insted of INPUT?
Kevin (x.t.r.GRAPHICS)

[Image: 11895-r.png]
Reply
#7
GOT IT!!!! Thanks to all of you who helped!!! Output WAS the key...it works great. Nice to know there's this safety net out here for those of us new to programming in any language besides LabVIEW. Thanks again.
Reply
#8
You're very welcome... Happy coding! :wink:
Kevin (x.t.r.GRAPHICS)

[Image: 11895-r.png]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)