A little problem with filing in C

  • Thread starter Thread starter Peon666
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around a problem with reading lines from a file in C for an SJF scheduling algorithm. Participants explore methods to read each line into separate arrays or pointers, addressing issues related to variable initialization, memory allocation, and code errors.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant seeks help on how to read each line from a file into separate arrays or strings.
  • Another participant suggests using an array of char pointers or a two-dimensional array, noting the need to know the maximum line length beforehand.
  • A participant confirms successful compilation and mentions that using char arrays for each line seems to work.
  • There is a question about a typo in the code regarding the file pointer variable.
  • A participant shares modified code but encounters issues with counting and outputting characters correctly.
  • Another participant advises using a debugger to trace variable values during execution and points out multiple errors in the shared code.
  • One participant expresses frustration with the output of their code, indicating confusion over the counter's behavior and the output of the array.
  • Another participant criticizes the code for containing errors and emphasizes the importance of variable initialization and proper syntax.

Areas of Agreement / Disagreement

Participants generally agree that there are issues with the code shared, but there is no consensus on the best approach to resolve the problems or on the correctness of the various proposed solutions.

Contextual Notes

Participants mention issues with variable initialization, memory allocation, and syntax errors in the code. There are unresolved questions about the behavior of counters and the output of arrays.

Peon666
Messages
107
Reaction score
0
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.
 
Technology news on Phys.org
Many ways to skin that cat.

Are all lines of the same length? Or different?

You can declare array of char pointers, read each line separately to a buffer, duplicate it and put the pointer to duplicate in the array.

Or you can declare two dimensional array (char[][]) large enough to accommodate all lines - that requires you to know maximum length before starting to work and can be very inefficient in tems of memory used.

Edit: have you tried to compile your program? Doesn't look like something correct.
 
yeah I've compiled it. I'm running it on Linux terminal and it's running fine. Thanks for the suggestion.

I've tried using char arrays for each lines and seems like it's working.
 
Code:
if (!f)
{
return 1;
}

What is f?
 
oh. It was fptr actually. typo error.
 
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?
 
Use debugger and trace how values of the variables change during execution.

Code you show here is full of errors. Are you retyping it? Can't you copy and paste?
 
Yeah, retyping. I'm compiling it on another PC and internet is having problems there, so I'm using net on another PC.

Ok, I'll try to debug and see.
 
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.
 
  • #10
Okay. I think using pointers would be a bad idea. Here the new code using only arrays:
Code:
int main ()
{
    FILE *fptr;
    arr[20];
    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++;
                 arr[count]=s[i];
                 i++;
            }
        }
    }
    for (i=0 i<count; i++)
    {
        printf ("%c", arr[i]);
    }
fclose(fptr);
return 0;
}

PROBLEM:

Only question: Why is the counter only 4 in the following loop?

Code:
 while (s[i] != '\n')
            {
                 count++;
                 arr[count]=s[i];
                 i++;
            }

File format is:

P1 0 (this is the first line. As the counter increments to only first, the array outputs in the final loop only P1 and not 0)
 
  • #11
Your code doesn't make sense - it contains errors, so it is impossible to analyse. i is used before being initialized, there is no such thing as arr[20] as it doesn't follow array declaration syntax and so on. Posting again and again and repeating the same invalid code you are only wasting time and it will get you nothing. I told you - trace all variables and see if they change as you think they do.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 2 ·
Replies
2
Views
9K
Replies
5
Views
2K
  • · Replies 65 ·
3
Replies
65
Views
8K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 11 ·
Replies
11
Views
35K
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K