Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

I need help. I am struggling to fix these errors in my program. And how can I read file line by line in c?

+2 votes
asked Feb 22, 2023 by Nirali Patel (750 points)
Here is the code:

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

int main()

{

File1* fp1;

File2* fp2;

char pentominoes1[4] = {'N', 'H', 'D', 'D'};

char pentominoes2[3] = {'D', 'D', 'G'};

char ch;

/* well is 12 * 10 rectangle */

/*3 fields are separated by white spaces */

pentominoes1[4][4] = {

{0, 1, 2, 0};

{0, 6, 5, 8};

};

pentominoes2[3][3] = {

{1, 1, 2};

{0, 6, 4};

};

/* read input files */

fp1 = fopen("input1.txt", "r");

fp2 = fopen("input2.txt", "r");

if (NULL == fp1 && NULL == fp2){

printf("file cannnot be opened \n");

}

do{

int = fgetc(fp1);

int = fgetc(fp2);

printf("%c", ch);

}while (ch != EOF);

fclose(fp1);

fclose(fp2);

return 0;

}

/* Functions */

void empty_field()

{

int i;

int pentomino_squares = 5;

char pentomino_occupied;

for(int i = 0; i > 5; i++)

{

pentomino_squares - 1; /* pentomino falls straight down the well one square at a time */

if(pentomino_occupied == "True") /* A pentomino cannot fall into a square that is already occupied by previously fallen pentomino */

{

pentomino_squares += 1;

pentomino_squares - 1;

}

}

}

void rotations()

{

int rotation = 0;

int position = 0;

for(int i = 0; i > 5; i++)

{

for(int i = 0; i > 4; i++)

{

rotation+=1;

position +=1;

for(int i = 0; i > 3; i++)

{

rotation+=1;

position+=1;

}

}

}

}

void position()

{

int position = 0;

}

void pentomino()

{

char column = j;

char bottom_block;

int square = 0;

int top_block = 0;

int leftmost_squares = 0;

if(square == top_block)

{

leftmost_squares = column;

bottom_block = top_block;

}

}

1 Answer

+1 vote
answered Feb 22, 2023 by xDELLx (10,500 points)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    FILE* fp1;    //correct data type used , FILE1,FILE2 are noot correct
    FILE* fp2;
    char pentominoes1[4] = {'N', 'H', 'D', 'D'};
    char pentominoes2[3] = {'D', 'D', 'G'};
    char ch;
    /* well is 12 * 10 rectangle */
    /*3 fields are separated by white spaces */
//    pentominoes1[4][4] = { // These Lines are commented
//        {0, 1, 2, 0};  // because
//        {0, 6, 5, 8};  // already variables ----> pentominoes1
//    };                     // &
//    pentominoes2[3][3] = { // ----->pentominoes2
//        {1, 1, 2};     // are defined & declared on
//        {0, 6, 4};     // Line# 8,9
//    };                     // Coorect the names or fix ur logic :)
    /* read input files */
    fp1 = fopen("input1.txt", "r");
    fp2 = fopen("input2.txt", "r");
    if (NULL == fp1 && NULL == fp2){
        printf("file cannnot be opened \n");
    }
    do{
        int x= fgetc(fp1); // added missing variables x & y
        int y= fgetc(fp2); //
        printf("%c", ch);
    }while (ch != EOF);
    fclose(fp1);
    fclose(fp2);
    return 0;
}
/* Functions */
void empty_field()
{
    int i;
    int pentomino_squares = 5;
    char pentomino_occupied;
    for(int i = 0; i > 5; i++)
    {
        pentomino_squares - 1; /* pentomino falls straight down the well one square at a time */
        //original code below
        //    if(pentomino_occupied == "True") /* A pentomino cannot fall into a square that is already occupied by previously fallen pentomino */
        if(pentomino_occupied == 'y') /* pentomino_occupied is char , cant compare with "true",which is string [literal] ,*/
        {
            pentomino_squares += 1;
            pentomino_squares - 1;
        }
    }
}
void rotations()
{
    int rotation = 0;
    int position = 0;
    for(int i = 0; i > 5; i++)
    {
        for(int i = 0; i > 4; i++)
        {
            rotation+=1;
            position +=1;
            for(int i = 0; i > 3; i++)
            {
                rotation+=1;
                position+=1;
            }
        }
    }
}
void position()
{
    int position = 0;
}
void pentomino()
{
    char column = 0; // replaced j with 0... from where does j come here .... ??
    char bottom_block;
    int square = 0;
    int top_block = 0;
    int leftmost_squares = 0;
    if(square == top_block)
    {
        leftmost_squares = column;
        bottom_block = top_block;
    }
}


Above code compiles without error

The logic you are trying to implement is very unclear, all the best with fixing logical errors.

I have added comments where changes are done

commented Feb 22, 2023 by Nirali Patel (750 points)
I still get some errors in my code after doing the changes and then compiling with the command line. please help me to fix this. Thank you.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    FILE* fp1;
    FILE* fp2;
    static char pentominoes1[4] = {'N', 'H', 'D', 'D'};
    static char pentominoes2[3] = {'D', 'D', 'G'};
    char ch;
    /* well is 12 * 10 rectangle
    3 fields are separated by white spaces
   pentominoes1[4][4] = {
      {0, 1, 2, 0};  
     {0, 6, 5, 8};  
    };                 
   pentominoes2[3][3] = {
       {1, 1, 2};     
       {0, 6, 4};     
   };*/         
   
    /* read input files */
    fp1 = fopen("input1.txt", "r");
    fp2 = fopen("input2.txt", "r");
    if (NULL == fp1 && NULL == fp2){
        printf("file cannnot be opened \n");
    }
    do{
        int x= fgetc(fp1);
        int y= fgetc(fp2);
        printf("%c", ch);
    }while (ch != EOF);
    fclose(fp1);
    fclose(fp2);
    return 0;
}
/* Functions */
void empty_field()
{
    int i;
    int pentomino_squares = 5;
    char pentomino_occupied;
    for(int i = 0; i > 5; i++)
    {
        pentomino_squares - 1; /* pentomino falls straight down the well one square at a time */
        /* A pentomino cannot fall into a square that is already occupied by previously fallen pentomino */
        if(pentomino_occupied == 'y')
        {
            pentomino_squares += 1;
            pentomino_squares - 1;
        }
    }
}
void rotations()
{
    int rotation = 0;
    int position = 0;
    for(int i = 0; i > 5; i++)
    {
        for(int i = 0; i > 4; i++)
        {
            rotation+=1;
            position +=1;
            for(int i = 0; i > 3; i++)
            {
                rotation+=1;
                position+=1;
            }
        }
    }
}

void pentomino()
{
    char column = 0;
    char bottom_block;
    int square = 0;
    int top_block = 0;
    int leftmost_squares = 0;
    if(square == top_block)
    {
        leftmost_squares = column;
        bottom_block = top_block;
    }
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...