-2

I'm really new to coding, and there are topics that I just can't understand and can't find a lot of informations about, I have to create a C program that opens and reads a file in which I have to type a NxN matrix, and after it reads it, the program has to find contiguous sequences of numbers in every column and line; but I can't do anything because I simply don' t understand what commands I have to use and how to use them to make my C program read the file, I tried to look around on the internet but I can't find a lot of informations for some reason. Can someone explain what steps I have to take?I really don't understand what commands there are

  • _fopen_, _fscanf_ several times, and finally _fclose_ – bruno Feb 16 '19 at 16:31
  • RTM: https://www.tutorialspoint.com/cprogramming/c_file_io.htm, https://stackoverflow.com/questions/3463426/in-c-how-should-i-read-a-text-file-and-print-all-strings – ForceBru Feb 16 '19 at 16:32
  • @Italian Soldier, welcome to StackOverflow. Not to be rude, but there is an overabundance of information on this topic, so I have a hard time believing you can't find a lot of information on the subject. A simple search for "how to read file in C" on here or google will give you all the information you need. Understanding it is a different matter. This question is too broad; please ask a more specific question, like if there is a certain command that you don't understand. Look at our [How to Ask](https://stackoverflow.com/help/how-to-ask) page so you can ask in a more specific way. – Dustin Nieffenegger Feb 16 '19 at 17:03

1 Answers1

1

At the most simple level, this should work for reading a simple text file:

// Open file
FILE *fp;
fp = fopen("StateInfo", "r");

// Make sure file actually opened
if(fp == NULL){
        printf("Error opening file\n");
        exit(1);
}

// Read file line by line and load into struct members until EOF
while (fgets(sInputString, 99, fp) != NULL){
        // Loop Code
}

// Close File
fclose(fp);

This assumes a file named "StateInfo" is located in the same directory as my main.c file.

  1. Open file
  2. Make sure file opened correctly
  3. Process file
  4. Close file
Jman
  • 187
  • 1
  • 3
  • 12