Thread: A question about arrays within loops

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    14

    A question about arrays within loops

    Hello all,

    I'm just newly registered, and I'm sure you get this a lot, but I have a question. I've looked across the message boards and I've found plenty of responses that are helpful, and have Google'd quite a bit on the side, but I just resolved to register for an account to ask some of you personally, since the question seems to be a little too specific for a Google search to solve.

    I'm trying to read in from a text file using "fscanf" and store each value into a couple of arrays using a "for" loop. Here's what the actual code looks like. I've tried to add comments to make things a little clearer if my summary was too vague. NOTE: The program isn't complete yet, but I'm only compiling one section to run at a time.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h> //not in use yet
    
    int main(void)
    {
        FILE *stress_text=fopen("c:\stress.txt","r");
        int count_array_store;                      //control variable for the "for" loop below
        int cat_1,cat_2,cat_3,cat_4,cat_5;          //not in use yet
        int a,b;                                    //variables for storing array value per element
        int location[998];                          //arrays for integer number limited to three digits
        double stress[998];                         //arrays for floating point number limited to three whole number digits
        for(count_array_store=0; count_array_store<=EOF; count_array_store++)
        {
                                 a=location[count_array_store];
                                 b=stress[count_array_store];
                                 fscanf(stress_text,"%i %lf\n",&a,&b);       // I'm not sure if I need these newline characters,
                                 printf("%i %lf \n",a,b);                    //but I wasn't really wanting to take any chances. I'm
        }                                                                    //somewhat shaky on my "fscanf" knowledge
        fclose(stress_text);
        getchar();
        return 0;
    }
    /*The problem at execution: segmentation fault. */
    The problem isn't in compilation; it's a segmentation fault. I'm aware that this is tied to impermissible access to memory, but I'm not quite sure where the loop is wrong, if that's the issue. Could someone tell me what's causing the error? I have a feeling it's right under my nose.

    Sorry. I know this is a newbie question. I'm just still a little unfamiliar with programming in general, let alone C. On that note, if there's any other way to do this that's more efficient or easier that's immediately obvious, I'm open to suggestions, though any input on the error at all would not only suffice but be greatly appreciated.

    Thank you for the time!

    -Casey

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well your first problem is your for loop...
    Code:
    for(count_array_store=0; count_array_store<=EOF; count_array_store++)
    Given that on most machines an int is 32bits and can hold numbers up to 2giga and EOF is usually defined as -1, that loop is going to run away on you...

    You need to find some way to test for end of file that doesn't involve crashing your system... Look up while() and feof() in your C documentation, think about it for a bit... you'll find a far better way to terminate your loop.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    14
    Good point. I didn't know what EOF was stored as. That was probably a very dumb maneuver on my part. The only reason that I tried the EOF value was because my initial attempt of 998 gave me a segmentation fault as well, though.

    I did attempt this with a "while" loop, but I couldn't quite get it down. In fact, I was getting a segmentation fault with that method, too. I'll keep trying it, though.

    [QUICK EDIT]: I changed my value back from EOF and tried it out a few times with some modification, but still getting seg. faults. "feof" looks to be the way to go, though. I'm trying it out now.
    Last edited by winterhawk787; 04-30-2011 at 06:15 PM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well, there's also the possibility that your file isn't opening. You really should check the returned value from fopen()... if it's NULL, you need to terminate the program with an error message.

    In fact, I'm pretty sure it's not... You should even be getting a compiler warning about an unknown escape character on that line...
    In C the \ is an escape character used to imbed simple text formatting \t for tab, \n for newline etc. To put a \ in a C string you need to use \\ ... so your correct file path would be "c:\\stress.txt".
    Last edited by CommonTater; 04-30-2011 at 06:18 PM.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    14
    I put a conditional in to check the status of the "stress.txt" file being read, and turns out it isn't, like you said. But I updated the path, and it still isn't being read. Is there something incorrect about my syntax with "fopen" maybe?

    Thanks again for the help, Tater. I appreciate it.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h> //not in use yet
    
    int main(void)
    {
        if(fopen("c:\\stress.txt","r")==NULL)       //conditional to check the status of the file
        {
                                 printf("Error reading file!");
                                 getchar();
                                 return 0;
        }
        else
        {
         FILE *stress_text=fopen("c:\\stress.txt","r");
        
         int count_array_store;                      //control variable for the "for" loop below
         int cat_1,cat_2,cat_3,cat_4,cat_5;          //not in use yet
         int a,b;                                    //variables for storing array value per element
         int location[998];                          //array for integer number limited to three digits
         double stress[998];                         //array for floating point number limited to three whole number digits
         for(count_array_store=0; count_array_store<=998; count_array_store++)
         {
                                 a=location[count_array_store];
                                 b=stress[count_array_store];
                                 fscanf(stress_text,"%i %lf\n",&a,&b);       // I'm not sure if I need these newline characters,
                                 printf("%i %lf \n",a,b);                    //but I wasn't really wanting to take any chances. I'm
         }                                                                    //somewhat shaky on my "fscanf" knowledge
         fclose(stress_text);
         getchar();
         return 0;
        }
    }

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    No worries... It would help if you could post your current code...

    While you're at it... make sure the file actually exists in the first place.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    14
    Quote Originally Posted by CommonTater View Post
    No worries... It would help if you could post your current code...
    I just edited it into the last reply. Sorry about that.

    The file exists, Tater. I'm new, but not that new. Haha. Although, I wouldn't be surprised if my referencing was wrong.
    Last edited by winterhawk787; 04-30-2011 at 06:37 PM.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by winterhawk787 View Post
    I just edited it into the last reply. Sorry about that.

    The file exists, Tater. I'm new, but not that new. Haha. Although, I wouldn't be surprised if my referencing was wrong.
    LOL... nice try but no prize this time... You're not initializing the file handle for your reads.
    Try it like this...
    Code:
    FILE *stress_test = NULL;
    
    stress_test = fopen("c:\\stress.txt","r");
    if (!stress_test)
      { printf("The $^$@$ file didn't open\n\n");
         exit (0); }
    
    // rest of code here

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    14
    Updated code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h> //not in use yet
    
    int main(void)
    {
        FILE *stress_text;
        stress_text=fopen("c:\\stress.txt","r");
        if(!stress_text)                             //conditional to check the status of the file
        {
                                 printf("Error reading file!");
                                 getchar();
                                 return 0;
        }
        else
        {
         int count_array_store;                      //control variable for the "for" loop below
         int cat_1,cat_2,cat_3,cat_4,cat_5;          //not in use yet
         int a,b;                                    //variables for storing array value per element
         int location[998];                          //array for integer number limited to three digits
         double stress[998];                         //array for floating point number limited to three whole number digits
         for(count_array_store=0; count_array_store<=998; count_array_store++)
         {
                                 a=location[count_array_store];
                                 b=stress[count_array_store];
                                 fscanf(stress_text,"%i %lf\n",&a,&b);       // I'm not sure if I need these newline characters,
                                 printf("%i %lf \n",a,b);                    //but I wasn't really wanting to take any chances. I'm
         }                                                                    //somewhat shaky on my "fscanf" knowledge
         fclose(stress_text);
         getchar();
         return 0;
        }
    }
    Compiled and ran, and still gave the "printf" statement under the conditional. No prize still, or am I completely disqualified?

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well... at least you're getting closer...
    1) Can you post a sample from the actual file you're trying to read? 5 or 10 lines should suffice.
    2) What on earth is this supposed to do...
    Code:
    a=location[count_array_store];
    b=stress[count_array_store];
    fscanf(stress_text,"%i %lf\n",&a,&b);                                    
    printf("%i %lf \n",a,b);

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The file doesn't exist or you don't have permission to open it. Put the file wherever your executable is and remove the path specification:
    Code:
    fp = fopen( "stress.txt", "r" );

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Apr 2011
    Posts
    14
    Also, just a quick note: I do plan on trying the "feof" function and a "while" loop after I can resolve the problem of reading the file... I'm sure that there are more efficient ways, and I'll probably open another session to see if I can run through them and figure one out.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    The file doesn't exist or you don't have permission to open it. Put the file wherever your executable is and remove the path specification:
    Code:
    fp = fopen( "stress.txt", "r" );
    Quzah.
    That's another possibility... but usually Windows won't block you from opening a file in C:\ ...

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    That's another possibility... but usually Windows won't block you from opening a file in C:\ ...
    I know, but if fopen is failing, it's one of those two possibilities. It's either not there, or he can't access it. There's not really any other option.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Apr 2011
    Posts
    14
    Quzah, this file is on the hard drive of my computer, and I'm on the administrator account; additionally, the file exists. I just put the executable on C:\, and removed the path specification in the "fopen", but I'm still getting the "printf" statement. I've always been able to run and edit things off of C:\ so it's not that I don't have permission, I don't think.

    Could it be the compiler?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays and Loops
    By sunandstars in forum C Programming
    Replies: 1
    Last Post: 02-17-2011, 04:34 PM
  2. Need help with for loops and arrays
    By Skeeter in forum C Programming
    Replies: 6
    Last Post: 10-23-2009, 03:33 PM
  3. while loops with arrays
    By volk in forum C Programming
    Replies: 2
    Last Post: 02-04-2003, 07:22 PM
  4. Arrays and loops
    By Zewu in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2002, 11:05 AM
  5. loops and arrays
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-03-2001, 03:12 PM