Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

C programming. Use a variable to save filename???

Status
Not open for further replies.

spitso

Member
Hi there

Im just a bit stuck atm;

im trying to save a structure into a .txt file, but i want the save name to be set by the user which is stored in a variable.

attached is my code but when i go to open the file it doesnt open correctly.

any ideas what ive done wrong?

thankyou
 

Attachments

  • code.png
    code.png
    16.4 KB · Views: 163
Are you in windows? Can you find the file in Windows Explorer? Is so, try to open it with a text editor, like note pad. If the file is there and correct, then you've saved it properly.
 
the problem is im using a program named putty, which saves the file to my school computers.
so i cannot open the file for normal viewing. but the files are accessible for use with putty.

other .txt files are stored there and my coding can open it.........just not the ones i created with my program

any one else have any ideas?
 
ive just tested it without using a variable to store the name and it work perfectly.........so everythings fine except using the variable as the file name
 
That's not a problem. Do a "dir" of your working directory and see of your file shows up. If it does, type "more <filename>" to view the file contents.
 
The problem might be the "fgets" statement. Try using "fscanf" instead.

PS: I'm sure using a vairable as the filename is no problem. But you can verify that for yourself by assigning a value in your program. ie


char var_name[256]

...

strcpy(var_name, "some_variable_name")
.....

fopen(var_name....)
 
Last edited:
Ok wow, can view the contents like that.

i just changed how i got the variable.....instead of fgets()...i just used scanf() and now it works fine.

thankyou for all the help BrownOut
 
oooohhh yeaaa totally forgot about the newline character


ok another quick problem is when printing the float character, it displays wrongly

displays 202.399994 instead of the actual 202.3
is there a way to fix this to display or even store the float to a given significant figure?

thanks
 
You format the printf statement with field width and # of significant digits. Look at the documentation for printf for details.
 
In the future, open your files like this:

Code:
if((fp=fopen("filename", "w")) == NULL)
  printf("Error opening file\n";
  exit(1);

That way if you have any errors in your filename or if there is any problem in the filesystem, you'll know it right away and won't have to wonder "what the hell happened!"
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top