Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Fopen Problem
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
bfh




PostPosted: Sat Aug 07, 2010 5:25 pm   Post subject: Fopen Problem

Heres my Current Code, Cant get this to work for some Reason, will keep
working on it till i can figure out or some 1 helps me out

Thanks.

code:

#include <stdio.h>
#include <stdlib.h>

int main() {
       
        /*Files To be Used Below*/
        FILE *stock.dat, *videos.dat;


       
        int stocknumArray[1000];

        stocknumArray = fopen("videos.dat", "r");
       
        for (i=0; i<120; i ++){
        printf(\nstocknumArray[i]);
        }

   fclose(videos.dat);
   return 0;

 }

Sponsor
Sponsor
Sponsor
sponsor
TheGuardian001




PostPosted: Sat Aug 07, 2010 5:41 pm   Post subject: Re: Fopen Problem

Fairly certain you aren't allowed to have . in a variable name...

You're also trying to store the result of fopen (of type FILE*) into an integer array (fopen doesn't read from the file, just opens it for reading.)

Quote:

code:

printf(\nstocknumArray[i]);



Not a valid printf statement. You seem to have sort of blended two almost functional printf statements into a completely incorrect one.
Did you mean to printf("\n"), and then printf("%d",nstocknumArray[i])?

code:

fclose(videos.dat);

Again, no . in variable names. You should be getting an "undeclared identifier" error here, since the declaration will have failed (due to the .)

EDIT: I'd like to point out that all I did was restate information given to me by the compiler when I ran your code. I'd advise you to look at your program's output if it doesn't compile.
bfh




PostPosted: Sat Aug 07, 2010 6:50 pm   Post subject: RE:Fopen Problem

These are the errors im getting.

>Your post didn't really help ~ but thanks
Tony




PostPosted: Sat Aug 07, 2010 8:07 pm   Post subject: RE:Fopen Problem

You didn't even ask any questions.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
TheGuardian001




PostPosted: Sat Aug 07, 2010 8:30 pm   Post subject: Re: Fopen Problem

Really? Pointing out all of the problems didn't help? You're that bad?
Alright, you asked for this one. Time to show you exactly how wrong you are (and demonstrate your inability to read compiler output.)

Let's see what GCC has to say about all your program:
Posted Image, might have been reduced in size. Click Image to view fullscreen.
Wow. That sure is a lot more than the 0 you claim to be getting.

Let's walk through this again:
Quote:

test2.c:7: error: syntax error before '.' token

You cannot have a period in a variable name. Change your variable names.

Quote:

test2.c:13: error: incompatible types in assignment

You are trying to store the result of fopen (which returns FILE*) in a variable of type int[1000]. This is not allowed. Instead, you should store it in one of the two FILE* variables you created earlier (once you change them to be valid variable names (IE, ones without a period in them))

Quote:

test2.c:15: error: 'i' undeclared identifier (first use in this function)
test2.c:15: error: (each undeclared identifier is reported only once
test2.c:15: error: for each function it appears in.)

You never declared i before using it in your for loop. depending on your compiler, use either
for (int i=0; i< 120, i++){}.
Or
int i; for (i=0; i<120;i++);

Quote:

test2.c:16: error: stray '\' in program
test2.c:16: error: 'nstocknumArray' undeclared (first use in this function)

Really, GCC should have been harder on you on this one.
code:

printf(\nstocknumArray[i])

Is not only a stray \, but also the worst printf statement I have ever had the displeasure of encasing in a CODE tag.
If you want a newline, then the value in stocknumArray[i] (assuming you actually read a value into that, instead of trying to put the result of fopen into it), you should instead use:
code:

printf("\n");
//Also, note the quotes around the newline here, so it's a const char *, instead of a syntax error.
printf("%d",stocknumArray[i])
//note how this is a valid printf, which provides a const char *, and the value to have printf place in the output.


Quote:

test2.c:19: error: 'videos' undeclared identifier (first use in this function)

Again, videos has not been declared, because you tried to declare it with a . in the variable name You also never used the incorrectly initialized videos.dat variable, instead trying to store the result of fopen in an int[1000].


SO
to sum up this entire post:
Your problem is not fopen (although you did that wrong too, trying to store FILE* in an int[1000].) Your problem is everything else.
bfh




PostPosted: Sat Aug 07, 2010 10:00 pm   Post subject: RE:Fopen Problem

Thanks For help, Figured it out.

also this link helps for anyone else trying to do this:

http://www.cplusplus.com/reference/clibrary/cstdio/fopen/

Sorry for all the confusion I tend to learn better by examples, but with a bit of explanation. Also if my question wasn't clear I'll try to make my next post more meaningful.
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: