![]() |
|
#1
|
|||
|
|||
creating a file in [c]Hi im new to c so please bare with me
basically i need to create a file containing 3 colomns of numbers under the titles id, x and y. the file will need to be modified later and the number of values it holds is unknown. below is basically what i want to do to the file but i dont know how to create it in the first place. Any help would be great thanks mick CPP / C++ / C Code:
|
|
#2
|
|||
|
|||
Re: creating a file in [c]Quote:
Since it's a text file, you could use a text editor. If you want to write a program to create a file, you create a new file for writing by using fopen(..., "w") CPP / C++ / C Code:
Regards, Dave P.S. As to your screen name: Attitude is a highly reflective thing. I have found over the years that if people really hate C, it really hates them right back. It's funny that way. |
|
#3
|
||||
|
||||
Re: creating a file in [c]Hi You_hate_c,
![]() You have a funny username. ![]() Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#4
|
|||
|
|||
Re: creating a file in [c]Cheers for the help Dave, a bit advanced for me, turns out all i really need is a couple of static arrays like these
CPP / C++ / C Code:
|
|
#5
|
|||
|
|||
Re: creating a file in [c]could you also explain what these lines mean in the program
CPP / C++ / C Code:
thanks mick |
|
#6
|
||||
|
||||
Re: creating a file in [c]Hi Mick,
Quote:
Here is a sample: Consider that you have this file: Code:
We have a delimeter as a space between the numbers. We can assume that the first column is id, the second column is x and then the third column as y. next, we have to read from the file and store it in the array. You can do that using fscanf function. Here is a sample function that scans the numbers from the file and stores it in floats: CPP / C++ / C Code:
Note how the input is got from the file: CPP / C++ / C Code:
You have to be very careful while writing the file using a text editor. Even a extra space will lead to runtime error. Now, you have got the input from the file. You can write to the file using the fprintf method, similar to the printf method. CPP / C++ / C Code:
and we are getting only two inputs. For getting all the inputs, you have to use a loop. Regarding this: CPP / C++ / C Code:
node is a structure with three elements one of integer, and two doubles. the elements can be accessed by using the "." operator. For example, consider that you have created a structure like: CPP / C++ / C Code:
node1.id represents the integer id. node1.x represents the double x. node1.y represents the double y. Basically, what should your program do? Only if you tell us that, we can tell about the no_nodes and other things. Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#7
|
||||
|
||||
Re: creating a file in [c]Quote:
Well your original post indicated that you were reading the data from a file. I suggested that you could create a file with a text editor, then read it into your program. What's advanced about that? Didn't you use a text editor to create your C program file? Quote:
How do you know that you need arrays like this? Was this part of the original program specification that someone didn't tell you or what? There are problems: 1. Assuming we are programming in C, the notation grid1.dat indicates that grid1 is a struct with a member dat that is an array[3][9] of something. On the other hand the notation float .... indicates that you are declaring a variable or array or something that has a floating point data type. 2. The initializer list for your first declaration indicates that it is an array[9][3] of something. The initializer list for your second declaration indicates that it is an array[12][3] of something. 3. Both declarations have a syntax error: missing right brace '}'. If you have a question about a program device or construct, I would suggest that you make a small program that uses that construct (a main() program that doesn't do anything but declare the variables) and compile it. If the compiler gives errors that you can't understand, then show us exactly what you compiled and the exact errors that the compiler gave and ask whatever it is that you don't understand. More generally, when writing a program, the very first question you should ask yourself is: "What is it that the program really needs to do?" What are the program's inputs? What are the program's outputs? What kind of processing is the program supposed on the inputs in order to get the outputs? I respectflully suggest that you not only ask yourself these questions, but that you write them down (and write down your answers to the question also). If you have a printer, maybe you can use a text editor instead of pencil and paper and print the questions and answers out so that you can refer to them as you develop the program. If the program is part of an assignment or contract or something maybe the problem assignment tells these things, so you don't actually have to create any new questions and answers. But make sure you understand what the program specification is in terms of input==>processing==>outputs Quote:
I don't understand what you mean by this. I see two questions: 1. How do I put these into the program? You put things into a program by putting them into the program (I'm not trying to be cute or sarcastic here---I really don't understand the question). 2. How do I keep the 3 headers id, x, and y at the top of the columns? Are you asking how to print something out? Your original post had no output statements at all, as I recall. Well, assuming that you mean to print out some stuff, here's the question that you ask yourself (and answer): What do you want the output to look like? I usually use printf(), and I look at what I want the output to look like and I repeatedly try different things in printf to make the columns line up (if that is what you are asking). Sometimes it's easier just to print things out in a messy way and then refine the formatting fields until I get the desired output. Sometimes it's easier to plan ahead and create a format statement that I think will give me what I want. I usually have to iterate a few times no matter how carefully I plan ahead. (I like things to look pretty, even if the program itself is a throw-away. Why? Well, I'm funny that way. Also, it's good to practice creating nice output to make it easier to do such things when I get to a point where the appearance of the output is critical to acceptance of the program.) Quote:
I don't see how you wrote the program that you showed in your original post without knowing this. The program itself was a pretty good effort at reading some things from a file. And it used properties of the things that you now ask about. However: The program contains a statement that defines a structure with the tag "node": CPP / C++ / C Code:
This has one integer member named "id" and two double members named "x" and "y". Now, consider the statement CPP / C++ / C Code:
This declares a variable named "node_array". The variable is an array of 100 structs of type node. Consider this statement: CPP / C++ / C Code:
This is the same as the two following statements: CPP / C++ / C Code:
The first declares an int variable named "no_nodes" and sets the initial value to zero. The second declares an int variable names "no_values" and doesn't set the initial value to anything (so its value is undefined until you set it equal to something --- with an assignment statement or sscanf() or something. Regards, Dave [/c] |
|
#8
|
|||
|
|||
Re: creating a file in [c]hey paramesh thanks for the help, basically the program should prompt the user to enter a file name. when grid1.dat is entered as the file name it should display a table like this
id x y 1 1.2 1.8 2 1 4.8 3 1.3 3.8 4 1.4 0.8 5 1.6 1.2 and when grid2.dat is entered it should display a second similar table. The user shouldnt have to enter any of the data, both tables should be stored in the program and the program look like it is shown in the first post |
|
#9
|
||||
|
||||
Re: creating a file in [c]Hi Mick,
Quote:
That would be simple. Just accept the filename from the user. By using scanf statement. CPP / C++ / C Code:
You can display the first line using printf statements. like: CPP / C++ / C Code:
Meanwhile, where did you get the code you posted first? Regards, ![]() Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#10
|
|||
|
|||
Re: creating a file in [c]thanks paramesh, its a university assignment so the lecturer gave me a program with some mistakes in it, had to correct all the errors in the program and make it look up those files.
how do i go about adding the file itself to the program CPP / C++ / C Code:
is this right and if so where abouts in the program should it be inserted? |
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 07:44 |
| CD Burner Help - Power Calibration Error.... | JonBoy420 | Computer Hardware Forum | 110 | 05-Feb-2008 18:34 |
| CD burner wont burn!! | robertli55 | Computer Hardware Forum | 1 | 18-Jun-2004 10:53 |
| Yet another CD burner problem: Lite-On LSC-24082K | Erwin | Computer Hardware Forum | 1 | 22-May-2004 11:28 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The