Thread: A little problem with filing in C

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    36

    A little problem with filing in C

    I'm writing an SJF scheduling algo and I'm having a little problem with filing. I'm to read data from a file. I've read the entire data in a single array/pointer but now I want to read every line in a separate array/pointer/string. I'm not able to do so. Ay help would be highly appreciated.

    Here's the code so far:

    Code:
    int main ()
    {
    FILE *fptr;
    int i;
    printf ("Output from the file");
    char s[500];
    int count = 0;
    fptr = fopen ("file.txt","r");
    if (!f)
    {
    return 1;
    }
    while (fgets(s,1000,fptr) != NULL)
    printf ("%s",s);
    fclose(fptr);
    return 0;
    }
    How can I read each line in a separate array/string/pointer?

    Thanks.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    First you have to make an array of arrays like so:

    Code:
    const int MAX_ARRAY_LEN = 100;
    
    int nr_arrays = 5;
    
    char *array[MAX_ARRAY_LEN];
    
    /* you have just declared a pointer to an array of length MAX_ARRAY_LEN */
    
    /* allocate memory for an array of arrays of length MAX_ARRAY_LEN using malloc()
    
    array = malloc((sizeof(char) * MAX_ARRAY_LEN) * nr_arrays);
    
    /* now use array[i] to store whatever you wish */

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by TheUmer
    Here's the code so far:
    I notice that you declare s with a size of 500, but in the fgets() call you use it as if it has a size of at least 1000.

    If it is reasonable for you to use an array of size 1000 to store each line, then a more correct version of what claudiu suggests would be:
    Code:
    #define LINE_MAX_SIZE 1000
    
    /* ... */
    
    char (*lines)[LINE_MAX_SIZE];
    size_t lines_size = 0;     /* number of lines stored */
    size_t lines_capacity = 4; /* realloc when lines_size would exceed lines_capacity */
    
    lines = malloc(sizeof(*lines) * lines_capacity);
    
    /* Now, fgets() in a loop with lines[i] and LINE_MAX_SIZE */
    Remember to use free() when you are done with lines.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    Alright. I'm still having a few problems. I tried to get each line in a separate pointer but it's getting crazy. Here's the code:

    Code:
    int main ()
    {
    FILE *fptr;
    int i;
    printf ("Output from the file");
    char s[500];
    int count = 0;
    fptr = fopen ("file.txt","r");
    if (!fptr)
    {
    return 1;
    }
    while (fgets(s,1000,fptr) != NULL)
    {
    if (s[i] == 'P' && s[i+1] == '1')
    {
    while (s[i] != '\n')
    {
    count++;
    i++;
    }
    }
    for (for i=0; i<count; i++)
    {
    p1= malloc (sizeof (char)*count);
    //printf ("%d", count);
    p[i] = s[i];
    }
    printf ("%s",s);
    }
    for (i=0 i<count; i++)
    {
    printf ("%c", p1[i]);
    }
    fclose(fptr);
    return 0;
    }
    PROBLEM:

    The value of the counter in the loop is 4 (4 elements in the first row), and it indeed gies the value of the counter 4 when I printf the counter. But in the following loop:

    Code:
    for (for i=0; i<count; i++)
    {
    p1= malloc (sizeof (char)*count);
    //printf ("%d", count);
    p[i] = s[i];
    }
    It outputs the counter 40 times. What's going on?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't think that can compile, with a for on the outside and a for on the inside of the parentheses.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    Would you kindly elaborate a bit more?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by TheUmer View Post
    Would you kindly elaborate a bit more?
    Not really, no. The code you posted has a syntax error and cannot compile. Therefore that is not the code you are using. I can't help much more than say "that's not the code you're using".

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    ????

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Look at this line:
    Code:
    for (for i=0; i<count; i++)
    Furthermore, you need to indent your code properly.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    Sorry that was a typo error. I'm compiling it on another PC and I don't have net on it.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by TheUmer View Post
    Sorry that was a typo error. I'm compiling it on another PC and I don't have net on it.
    Well, that's fine and all, but we can't actually tell you what's going on with the code unless we can see the actual code.

  12. #12
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    What do you mean by 'actual' code?

  13. #13
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    Here's a bit modification:

    Code:
    while (fgets(s,1000,fptr) != NULL)
    {
        if (s[i] == 'P' && s[i+1] == '1')
        {
           while (s[i] != '\n')
           {
                count++;
                p1=malloc (sizeof(char)*count);
                p1[count-1]=s[i];
                //printf ("%c", p1[i]);
                i++;
                }
            }
            for (i=0 i<count; i++)
            { 
                 printf ("%c", p1[i]);
            }
            fclose(fptr);
            return 0;
    }
    Now the counter is running fine and it's putting values in p1, but problem is in the last loop all p1 outputs in 0 four times.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    One way is to copy and paste verbatim, though that would be a little difficult considering that it is from a different computer. Perhaps use a thumb drive to transfer the files, and then copy and paste from that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by TheUmer View Post
    Here's a bit modification:

    Code:
    while (fgets(s,1000,fptr) != NULL)
    {
        if (s[i] == 'P' && s[i+1] == '1')
        {
           while (s[i] != '\n')
           {
                count++;
                p1=malloc (sizeof(char)*count);
                p1[count-1]=s[i];
                //printf ("%c", p1[i]);
                i++;
                }
            }
            for (i=0 i<count; i++)
            { 
                 printf ("%c", p1[i]);
            }
            fclose(fptr);
            return 0;
    }
    Now the counter is running fine and it's putting values in p1, but problem is in the last loop all p1 outputs in 0 four times.
    The actual code is just that, the actual code that you are running, not what you remember about your code some time later.

    As to this code, it is horrific. You leak heaven knows how much memory; you keep allocating more and more space for p1 (destroying the previous values each time), and then putting just one character in the new array. Most of your p1 array will end up being --- well, not blank exactly, but containing who-knows-what.

    (Of course, this analysis is only valid insofar as what you've posted matches what you've actually got -- again, there is a typo that would make this impossible to compile.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM