itlbok333
02-08-06, 07:22 PM
I am new to C and have just started taking a programming class. Can anyone assist me with this programming exercise I found in the back of my chapter on inout-output?
Write C code to read the following text input file of integers, and output them into another text output file as shown:
Input File: 1 2 3 4 5 6 7 8 9 10
Output File:
1
2
3
4
5
6
7
8
9
10
domesticated om
02-08-06, 07:47 PM
Hmmmm----I'm also a noob, and....well.... I'm not programming in C either (I start on C# next), but I do know how I would do this in Vb.net
If would look something like
'Defines the input
Dim bunchanumbers AS New ArrayList()
Dim numberlist() As String
Dim output As String
bunchanumbers.add("1")
bunchanumbers.add("2")
bunchanumbers.add("3")
bunchanumbers.add("the rest of the numbers")
'convert to concatenated output
numberlist = bunchanumbers.ToArray(Type.GetType("System.String"))
output = Join(numberlist, vbnewline)
Messagebox.Show(output)
Now all you have to do is figure out how to say the same thing in C
just stick the numbers into an array, then prinf the numbers with a \n between each element of the array. something like: printf("%d\n%d\n%d...." ,r[1],r[2],r[3]...);
I normally don't help people with homework, but C is hard if you don't have a starting point.
#include < stdio.h >
int main(int argc, char* argv[])
{
int temp;
int i;
FILE* fp = fopen(argv[1], "r");
for (i=0; i<10; i++)
{
fscanf(fp, "%d", &temp);
printf("%d\n\n", temp);
}
fclose(fp);
return 0;
}
christ AntonK:), its obviously a homework problem, you should not have solved it out for him. explain the concept to be used, and let him figure out the programming. :)
I fully realized that. The fact is I answered it to get the thread closed as quickly as possible. If the student cannot do that simple of a problem, he'll fail on his own. No help needed from me.
-AntonK
Can anyone assist me with this programming exercise I found in the back of my chapter on inout-output?
The chapter on input-output would probably help. Unless it's a really lousy textbook it should include examples. Once you understand the examples, an exercise like this should be easy.