Question

write a function in C that reads and saves the following from a file into A[9][9]....

write a function in C that reads and saves the following from a file into A[9][9]. Save '-' as 0.

-32-8-5
7----4--1
23---7-6-
-4--1----
57--492--
--3---64-
--------7
-5----1--
31-758--6
0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

#include <stdio.h>
void readFile(int A[9][9],char fileName[])
{

FILE* file = fopen(fileName, "r"); /* should check the result */
char line[256];
int i=0;
int k;
for(i=0;i<9;i++)
{
for(k=0;k<9;k++)
{
A[i][k]=0;
}
}
i=0;
while (fgets(line, sizeof(line), file)) {
/* note that fgets don't strip the terminating \n, checking its
presence would allow to handle lines longer that sizeof(line) */
int j;
for(j=0;j<strlen(line);j++)
{
if(line[j]!='-')
A[i][j]=(line[j]-'0');
else
A[i][j]=0;
}

i++;
}

/* may check feof here to make a difference between eof and io failure -- network
timeout for instance */

fclose(file);
int j;
printf("Contents of the array are: \n");
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
printf("%d ",A[i][j]);
}
printf("\n");
}
}
int main(int argc, char* argv[])
{
int A[9][9];
char fileName[800]; /* should check that argc > 1 */

printf("Enter file name: ");
scanf("%s",fileName);
readFile(A,fileName);
return 0;
}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
write a function in C that reads and saves the following from a file into A[9][9]....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How do I set up my read function? here's my code so far: int read_sudoku_board(const char file_name[], int board[9][9]) { FILE * fp = fopen("sudoku.txt", "r"); int a,i,j,c;...

    How do I set up my read function? here's my code so far: int read_sudoku_board(const char file_name[], int board[9][9]) { FILE * fp = fopen("sudoku.txt", "r"); int a,i,j,c; int count = 0; for(i = 0; i < a; i++){ for(j = 0;j < 9;j++){ c = fgetc(fp); if(c == '-'){ board[i][j] = 0; } else if(c >= 1 && c <= 9) printf(" %d"); else return -2; } count++; } if(count != a-1) return -1; else return 0; }12 Read...

  • Write a program that reads the integer numbers in a text file (numbers.txt) and stores the...

    Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4 5 6 7 8 9 0 7. Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4 5 6 7...

  • Write a program that reads the integer numbers in a text file (numbers.txt) and stores the...

    Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4 5 6 7 8 9 0 IN JAVA PLZ 7. Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4...

  • Lab 10 Write a program that reads the integer numbers in a text file (numbers.txt) and...

    Lab 10 Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). The number.txt file contains the following numbers: 1 2 3 4 5 6 7 8 9 0

  • 1. Write a C++ program that reads sides of a triangle a, b and c from a file and computes the are...

    1. Write a C++ program that reads sides of a triangle a, b and c from a file and computes the area of a triangle and counts total number of areas program has computed Program should have two functions sfun(): sfun receives the values of a, b and c then computes and returns the value ofs as areafun 0: areafun receives the value of s and computes and return the area of a triangle to main program using below formulae...

  • JAVA Write a program that 1. opens a text file 2. reads lines of text from...

    JAVA Write a program that 1. opens a text file 2. reads lines of text from the file 3. Converts each line to uppercase 4. saves them in an ArrayList 5. Writes the list of Strings to a new file named "CAPSTEXT.txt" Mainjavo ext.txt Instructions from your teacher 1 My mistress' eyes are nothing like the sun; 2 Coral is far more red than her lips' red; 3 If snow be white, why then her breasts are dun; 4 If...

  • Lab 10 Write a Java program that reads the integer numbers in a text file (numbers.txt)...

    Lab 10 Write a Java program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat).The number.txt file contains the following numbers: 1 2 3 4 5 6 7 8 9 0

  • help me please Q1 (10 pts) Write a java program that reads and saves 10 values...

    help me please Q1 (10 pts) Write a java program that reads and saves 10 values from the user in an array. Then calls function smallestDistance The smallest Distance function will finds two neighboring numbers in an smallest distance to each other. The function should return the index of the first number In the sequence 4 8 6 12 9 4 the minimum distance is 1 (between 1 and 2). The function should return the index 3 (of number 1).

  • How to write a Java file out that that reads from numbers.txt with these numbers 2...

    How to write a Java file out that that reads from numbers.txt with these numbers 2 6 7 9 5 4 3 8 0 1 6 8 2 3 and write to a file called totalSum.txt that looks like: 2+6+7+9=24 and so on using this code import java.io.*; import java.util.Scanner; public class FileScanner {    public static void main(String[] args)    {        /* For Homework! Tokens*/        Scanner file = null;        PrintWriter fout= null;   ...

  • //Done in C please! //Any help appreciated! Write two programs to write and read from file...

    //Done in C please! //Any help appreciated! Write two programs to write and read from file the age and first and last names of people. The programs should work as follows: 1. The first program reads strings containing first and last names and saves them in a text file (this should be done with the fprintf function). The program should take the name of the file as a command line argument. The loop ends when the user enters 0, the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT