Thread: File reading program not working

  1. #16
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I have no idea what kind of C - standard your compiler supports.
    This are the warnings of gcc
    Code:
    rf.c: In function 'main':
    rf.c:13:5: warning: ISO C90 forbids variable length array 'temperatureArray' [-Wvla]
    rf.c:20:37: warning: C++ style comments are not allowed in ISO C90 [enabled by default]
    rf.c:20:37: warning: (this will be reported only once per input file) [enabled by default]
    rf.c:13:11: warning: unused variable 'temperatureArray' [-Wunused-variable]
    rf.c: In function 'readingFunction':
    rf.c:42:5: warning: ISO C90 forbids mixed declarations and code [-pedantic]
    rf.c:52:13: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    rf.c:52:13: warning: statement with no effect [-Wunused-value]
    rf.c:67:1: warning: control reaches end of non-void function [-Wreturn-type]
    rf.c:48:9: warning: 'numberOfReadings' is used uninitialized in this function [-Wuninitialized]
    To try to resolve the fp issue I would try to move the srand() call in main() down one line.

    Kurt

  2. #17
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    Im using visual studio express

    Thanks ill try that and see if it works n get back to you

  3. #18
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by David_Baratheon View Post
    well you told me the code i provided was meaningless and couldnt tell you anything and then you proceeded to tell me whats wrong with you code lol
    I only pointed out what is obviously wrong with the code you provided. I have no way of working out what is wrong in the code you haven't shown. I also have no way of working out if your problem is due to some interaction between code you supplied, and code you didn't.

    Quote Originally Posted by David_Baratheon View Post
    I do have #include <stdio.h> in my preprocessing directives, can you think of any other reason why it doesnt like this?
    Plenty of possible reasons. One is that you are #define'ing a macro that interferes with your code (or the system headers) in some way.

    Quote Originally Posted by David_Baratheon View Post
    so your basically saying from what you can see from the code i provided, it should work, ive not utilised the file access techniques incorrectly?
    The other point of my post is that you are using uninitialised variables to provide the termination condition for a loop. It doesn't matter if the loop does operations on files or it does anything else. The loop construct itself is invalid.

    I will also bet that the inner loop ("for(flag=0; flag=1; 0)" does not do what you expect. If you want to compare flag with a value of 1, use two equals signs, not one.

    The fact you are not checking the return value from functions like fopen() and fscanf() does not help. If an error occurs in those functions, it is reported using the return value.

    Quote Originally Posted by David_Baratheon View Post
    ill try constructing a small program that gives full context to the code but it does seem that its very difficult to find a way to post here without people finding some way of getting onto their high horse and moaning. There should be some agreed ettiquette on how to post a problem so that noone is allowed to moan and is only allowed to post actual help with the programming problem
    The general etiquette in forums is that those asking questions need to ensure their question is as clear and complete as possible, without reliance on incidental mindreading skills in those who read the question. People in forums who devote time to answering questions are volunteers, not slave labour. The more difficult it is for someone to understand a question that you ask, the less likely you are to receive a useful answer. Or, to put it another way, your chances of getting a useful answer increase when you put more effort into asking a clear question, and providing enough (but not too much) relevant code.

    In your case, you have not provided enough information about your problem. So, the only help you get will be best guesses.
    Last edited by grumpy; 05-08-2012 at 07:02 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #19
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    I tried all the changes and nothing is working. Ive shortened the code, removed fuctions loops etc, strip the code to bare minimum and it still doesnt like the FILE *fp;

    Can anyone think why it might not like it?

  5. #20
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Post your modified complete program and the complete error messages, exactly as they appear in your development environment. Without this information how do you expect anyone to be able to help? Do you think we can magically see what you are doing?

    You should also re-read post #18 for helpful insights.

    Jim

  6. #21
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Try posting a small sample of code that actually illustrates your problem. In the process of creating such a sample of code, you might also have a "aha!" moment, and realise what the problem is. If not, and someone can get the behaviour you describe with their compiler, there is a chance they can identify the problem.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #22
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    Ok grumpy i will post some full code in about 30mins and we'll go from there

  8. #23
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    Jim im on my iphone i cant post code till i get to uni but first thing ill do is post up code n errors when i get thete

  9. #24
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    ok here is the code stripped down to its bare minimum. I have even commented out any of the file manipulation stuff so this shows that its the file pointer itself which isnt working:

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int main(void)
    {
        int loop;
        int signalOutput;
        int flag;
        int column1;
        float column2;
        float temperatureArray[10];
        int quit;
    
    
        srand(time(NULL));
    
    
        FILE *fp;    
    
    
        printf("\n\n The sensor readings and their corresponding temperatures:\n");
        
            fp = fopen("c:\\LOOKUP.txt", "r");
            
            /*
            
            for(loop=0; loop=10; loop++)
            {
                signalOutput = (int)(rand()%15);
                
                for(flag=0; flag=1; 0)
                {
                    fscanf(fp, "%i %f",&column1, &column2);
                    
                    if ( signalOutput > column1 ) 
                    {
                        temperatureArray[loop] = column2;
                        flag = 1;
                        printf("\n se %i = %f", signalOutput, temperatureArray[loop]);
                    }            
                }
            }
                
            */
        fclose(fp);
    
    
        printf("Press enter to quit");
        scanf("%i", &quit);
    
    
        return 0;
    }

    content of LOOKUP.txt

    0 10
    1 12
    2 14
    3 16
    4 18
    5 20
    6 21
    7 22
    8 23
    9 24
    10 24.5
    11 25
    12 25.25
    13 25.5
    14 25.75
    15 26




    run errors from visual studio express:


    1>------ Build started: Project: Assignment 3, Configuration: Debug Win32 ------
    1> testingarea.c
    1>c:\users\david baratheon\documents\visual studio 2010\projects\assignment 3\assignment 3\testingarea.c(16): warning C4244: 'function' : conversion from 'time_t' to 'unsigned int', possible loss of data
    1>c:\users\david baratheon\documents\visual studio 2010\projects\assignment 3\assignment 3\testingarea.c(18): error C2275: 'FILE' : illegal use of this type as an expression
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(66) : see declaration of 'FILE'
    1>c:\users\david baratheon\documents\visual studio 2010\projects\assignment 3\assignment 3\testingarea.c(18): error C2065: 'fp' : undeclared identifier
    1>c:\users\david baratheon\documents\visual studio 2010\projects\assignment 3\assignment 3\testingarea.c(22): error C2065: 'fp' : undeclared identifier
    1>c:\users\david baratheon\documents\visual studio 2010\projects\assignment 3\assignment 3\testingarea.c(22): warning C4047: '=' : 'int' differs in levels of indirection from 'FILE *'
    1>c:\users\david baratheon\documents\visual studio 2010\projects\assignment 3\assignment 3\testingarea.c(44): error C2065: 'fp' : undeclared identifier
    1>c:\users\david baratheon\documents\visual studio 2010\projects\assignment 3\assignment 3\testingarea.c(44): warning C4047: 'function' : 'FILE *' differs in levels of indirection from 'int'
    1>c:\users\david baratheon\documents\visual studio 2010\projects\assignment 3\assignment 3\testingarea.c(44): warning C4024: 'fclose' : different types for formal and actual parameter 1
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Last edited by David_Baratheon; 05-08-2012 at 08:54 AM.

  10. #25
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You are using the Microcrap compiler so you must define all variables at the beginning of the scope, before any function calls or equations.
    Code:
        int quit;  // Variable definition.
        srand(time(NULL)); // Function call.
        FILE *fp; // Variable definition, not allowed here with C90 compiler.
    You need to include the proper include files for the functions, data structs you are using. You need the stdlib.h header for srand(), and stdio.h for printf() and fopen().

    Is your input file actually at the root directory of your "C" drive? That's not a very good place to place any files. It should probably be in your project directory.
    Code:
    fp = fopen("c:\\LOOKUP.txt", "r"); // Open file in root of C:
    Jim

  11. #26
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    sorry ive just realised i cut the top 3 preprocessor directives out by accident when copy and pasting so ive edited it and put the first three in:

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


    I put it in the c drive just to make sure it wasnt a problem with how i was directing the pointer to the file. It was in the project folder before. was just trying to rule that out

    what do you mean concerning the functions? Our instructor told us not to use global variables, local variables should work fine usually?

    Or do you mean put the FILE and seed declarations before the variable declarations?

  12. #27
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    IT WORKED!!!

    I changed it like this:

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int main(void)
    {
    	FILE *fp;	
    	int loop;
    	int signalOutput;
    	int flag;
    	int column1;
    	float column2;
    	float temperatureArray[10];
    	int quit;
    
    
    	srand(time(NULL));
    
    
    	
    
    
    	printf("\n\n The sensor readings and their corresponding temperatures:\n");
    	
    		fp = fopen("c:\\LOOKUP.txt", "r");
    		
    		/*
    		
    		for(loop=0; loop=10; loop++)
    		{
    			signalOutput = (int)(rand()%15);
    			
    			for(flag=0; flag=1; 0)
    			{
    				fscanf(fp, "%i %f",&column1, &column2);
    				
    				if ( signalOutput > column1 ) 
    				{
    					temperatureArray[loop] = column2;
    					flag = 1;
    					printf("\n se %i = %f", signalOutput, temperatureArray[loop]);
    				}			
    			}
    		}
    			
    		*/
    	fclose(fp);
    
    
    	printf("Press enter to quit");
    	scanf("%i", &quit);
    
    
    	return 0;
    }

    and now it works!!!

    thats so counter intuitive that this would produce a problem, I dont think I would have got that in a million years if you didnt mention it.

    Thanks, much appreciated

    now Ill see if I can get the commented out code to work too ....

  13. #28
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You should be checking the return value of the fopen() call to insure the file is opening correctly.

    Jim

  14. #29
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Get rid of Microsoft VS if you are a beginner. I said it over and over again, it's like using a Boeing 747 to carry groceries. That IDE has far too many features for your limited understanding of programming and programming tools at the moment. It is not increasing your productivity, it is hammering it because you don't know the differences between compilers, how builds and linkage really work, etc. Start with a simpler IDE, or better yet, with a command line compiler and a basic text editor. Learn to write makefiles to build your program.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  15. #30
    Registered User
    Join Date
    Feb 2012
    Posts
    116
    which do you recommend?

    I only use VS because my university uses it and has it installed and teaches us with it and likes us to demonstrate our work on it, but my module is nearly finished so am happy to take up your suggestions and would love to learn more about

    " differences between compilers, how builds and linkage really work, etc"

    but most texts dont seem to go into this and only explain syntax.


    Im still struggling to get the next bit to work, is it ok to post up what ive got to see how to get it to work? dont want to take up too much of everyones time and you've already helped me big time as id never have spotted the error, it was quite obscure IMO

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a file not working
    By dabbertorres in forum C++ Programming
    Replies: 5
    Last Post: 01-21-2011, 04:31 PM
  2. why .exe file is not working in windows of c program
    By suryak in forum C Programming
    Replies: 9
    Last Post: 01-07-2011, 12:23 PM
  3. reading file then if statement not working
    By patrink in forum C Programming
    Replies: 3
    Last Post: 12-17-2010, 01:48 AM
  4. my serial port data reading program isn't working
    By gnychis in forum C Programming
    Replies: 5
    Last Post: 06-02-2005, 08:40 AM
  5. Getting the name of a file reading to program
    By DarkAbyss in forum C++ Programming
    Replies: 1
    Last Post: 03-22-2003, 10:14 AM