GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Nov-2005, 06:52
i hate c i hate c is offline
New Member
 
Join Date: Nov 2005
Posts: 16
i hate c is on a distinguished road

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:
char line[101], filename[101];
char *line_ptr;
struct node
	{
	int id;
	double x, y;
	};
int main(void)
{
struct node node_array[100];
int no_nodes = 0, no_values;
FILE *input_stream;
fprintf(stdout, "Enter file name:");
fscanf(stdin, "%s", filename);
if ((input_stream = fopen(filename, "r")) != NULL)
	{
	fgets(line, sizeof(line), input_stream);
	while (((line_ptr = fgets(line, sizeof(line), input_stream)) != NULL) &&
		((no_values = sscanf (line, "%d %lf %lf",
							&node_array[no_nodes].id,
							&node_array[no_nodes].x,
							&node_array[no_nodes].y)) == 3)) no_nodes++;
	if ((line_ptr != NULL) && (no_values != 3))
		fprintf(stdout, "Error reading line %s\n", line);
	else
		if (line_ptr == NULL)
			fprintf(stdout, "End of file found\n");
	}

return(0);
}
  #2  
Old 16-Nov-2005, 07:06
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: creating a file in [c]


Quote:
Originally Posted by i hate c
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

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:

 FILE *outfile;
 char *outname = "myoutputfile.txt";
 outfile = fopen(outname, "w");
 if (!outfile) {
  printf("There was a problem opening %s for writing\n", outname);
 }
 else {
 /* write to the file, or whatever */
 }


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  
Old 17-Nov-2005, 07:14
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

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  
Old 18-Nov-2005, 04:49
i hate c i hate c is offline
New Member
 
Join Date: Nov 2005
Posts: 16
i hate c is on a distinguished road

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:
float grid1.dat[3][9] = {{1, 1.2, 1.8}, {2, 1.2, 1.0}, {3, 1.2, 1.2}, {4, 1.4, 0.8}, {5, 1.4, 1.0}, {6, 1.4, 1.2}, {7, 1.6, 0.8}, {8, 1.6,1.0}, {9, 1.6, 1.2};

float grid2.dat[3][12] = {{1, 1.2, 1.8}, {2, 1.2, 1.0}, {3, 1.2, 1.2}, {4, 1.4, 0.8}, {5, 1.4, 1.0}, {6, 1.4, 1.2}, {7, 1.6, 0.8}, {8, 1.6,1.0}, {9, 1.6, 1.2}, {10, 1.8, 0.8}, {11, 1.8, 1.0}, {12, 1.8, 1.2};
but how do i put these into the program and keep the 3 headers id, x, and y at the top of the colomns?
  #5  
Old 18-Nov-2005, 05:58
i hate c i hate c is offline
New Member
 
Join Date: Nov 2005
Posts: 16
i hate c is on a distinguished road

Re: creating a file in [c]


could you also explain what these lines mean in the program

CPP / C++ / C Code:
struct node node_array[100];
int no_nodes = 0, no_values;

thanks
mick
  #6  
Old 18-Nov-2005, 07:09
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: creating a file in [c]


Hi Mick,

Quote:
Originally Posted by mick
but how do i put these into the program and keep the 3 headers id, x, and y at the top of the colomns?
There is no need for putting these in the top of a column.

Here is a sample:
Consider that you have this file:
Code:
1 1.2 1.8 2 1 4.8 3 1.3 3.8 4 1.4 0.8 5 1.6 1.2

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:
     int id;
     double x, y;     

     FILE *infile;
     char *inname = "asdf.txt";
     
     infile = fopen(inname, "r");
     
     if (!infile) 
     {
       printf("There was a problem opening %s for reading\n", inname);
     }
     else 
     {
            fscanf (infile, "%d %lf %lf\n", &id, &x, &y);
            printf("%d, %lf, %lf\n", id, x, y);
            
            fscanf (infile, "%d %lf %lf\n", &id, &x, &y);
            printf("%d %lf %lf\n", id, x, y);                        
     }
     

Note how the input is got from the file:
CPP / C++ / C Code:
            fscanf (infile, "%d %lf, %lf\n", &id, &x, &y);
the "%d %lf %lf " represents that there is a , and a space between the numbers. and the last \n represents the line break.
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:
            fprintf (outfile, "%d %lf %lf\n", id, x, y);
Note that this is just a sample program.
and we are getting only two inputs.
For getting all the inputs, you have to use a loop.

Regarding this:
CPP / C++ / C Code:
struct node node_array[100];
int no_nodes = 0, no_values;

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:
struct node node1;
Then, you can access the structure elements like:
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  
Old 18-Nov-2005, 07:39
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: creating a file in [c]


Quote:
Originally Posted by i hate c
Cheers for the help Dave, a bit advanced for me

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:
Originally Posted by i hate c
all i really need is a couple of static arrays like these

CPP / C++ / C Code:
float grid1.dat[3][9] = {{1, 1.2, 1.8}, {2, 1.2, 1.0}, {3, 1.2, 1.2}, {4, 1.4, 0.8}, {5, 1.4, 1.0}, {6, 1.4, 1.2}, {7, 1.6, 0.8}, {8, 1.6,1.0}, {9, 1.6, 1.2};

float grid2.dat[3][12] = {{1, 1.2, 1.8}, {2, 1.2, 1.0}, {3, 1.2, 1.2}, {4, 1.4, 0.8}, {5, 1.4, 1.0}, {6, 1.4, 1.2}, {7, 1.6, 0.8}, {8, 1.6,1.0}, {9, 1.6, 1.2}, {10, 1.8, 0.8}, {11, 1.8, 1.0}, {12, 1.8, 1.2};

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:
Originally Posted by i hate c
but how do i put these into the program and keep the 3 headers id, x, and y at the top of the colomns?

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:
Originally Posted by i hate c
could you also explain what these lines mean in the program

CPP / C++ / C Code:
struct node node_array[100];
int no_nodes = 0, no_values;

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:
struct node
	{
	int id;
	double x, y;
	};

This has one integer member named "id" and two double members named "x" and "y".

Now, consider the statement
CPP / C++ / C Code:
struct node node_array[100];

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:
int no_nodes = 0, no_values;

This is the same as the two following statements:

CPP / C++ / C Code:
int no_nodes = 0;
int no_values;

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  
Old 18-Nov-2005, 07:51
i hate c i hate c is offline
New Member
 
Join Date: Nov 2005
Posts: 16
i hate c is on a distinguished road

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  
Old 18-Nov-2005, 08:55
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough
Thumbs up

Re: creating a file in [c]


Hi Mick,
Quote:
Originally Posted by i hate 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

That would be simple.
Just accept the filename from the user.
By using scanf statement.
CPP / C++ / C Code:
char *infile;

printf("Enter the file...");
scanf("%s",infile);

infile = fopen(inname, "r");

/*code continues*/
So, you can use like this to get the filename and play around with the previous code i posted.

You can display the first line using printf statements.
like:
CPP / C++ / C Code:
printf("id  x  y\n");
for the first line and continue with getting the input from the file and then print the values...

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  
Old 18-Nov-2005, 10:16
i hate c i hate c is offline
New Member
 
Join Date: Nov 2005
Posts: 16
i hate c is on a distinguished road

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:
float grid1.dat[3][9] = {{1, 1.2, 1.8}, {2, 1.2, 1.0}, {3, 1.2, 1.2}, {4, 1.4, 0.8}, {5, 1.4, 1.0}, {6, 1.4, 1.2}, {7, 1.6, 0.8}, {8, 1.6,1.0}, {9, 1.6, 1.2};

is this right and if so where abouts in the program should it be inserted?
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 20:38.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.