Question

In: Computer Science

The following is for C programming language: I want to scan for initials in a line...

The following is for C programming language:

I want to scan for initials in a line of text. my line of text is as follows:

12345 3.5000 a j

12346 4.1000 s p

The first number represents the student ID, the second number represents the gpa, the third character represents the first initial and the fourth character represents the last initial of the student. My text file contains these values. The following is my code:

fscanf(fp, "%d %c %c", &studentID, &i1, &i2);

I only want to set the values up for studentID and initials. However, when I use the code, it shows that my student ID is right the first time, but it sets my initials as 3., then it says my student ID is 5000, and it sets the next initials as 5000. I am having a bit of trouble here. If you could help me, that would be great!

My general code displays this to the user:

==========================
||Student ID | Student Initials ||
||-----------------|------------------------||
|| 12345 | 3. ||
||-----------------|------------------------||
|| 50000 | aj ||
||----------------|------------------------||

Thank you!

Solutions

Expert Solution

fscanf is used to read files which have a structured format. So the format that we pass as input to fscanf function is the structure of one line in our file.

In our case, file has the following structure -

StudentID GPA Initial1 Initial2

Though we need only the Student ID and Initials in our display, we need to pass the entire structure while reading the file using fscanf, so that it can read correctly.

fscanf(fp, "%i %f %c %c", &studentID, &gpa, &i1, &i2)

Once we have read the entire line, we may just use those variables that are required in our display.

An example program has been shared below.

#include <stdio.h>

int main()
{
   FILE *fp = fopen("students.txt", "r");
   int studentID;
   float gpa;
   char i1, i2;
   if (fp == NULL)
   {
       return 1;
   }
   printf("===================================\n");
   printf("|| Student ID | Student Initials ||\n");
   while (fscanf(fp, "%i %f %c %c", &studentID, &gpa, &i1, &i2) != EOF)
   {
       printf("||--------------|------------------||\n");
       printf("||\t%i\t| \t %c %c \t ||\n", studentID, i1, i2);
   }
   printf("||--------------|------------------||\n");


   fclose(fp);
}

Screenshot

Output


Related Solutions

In C programming language how do you find if all the character on a single line...
In C programming language how do you find if all the character on a single line in a 2 dimensional array are the same? The program should read line by line to check if all the characters on a line are the same. For this program we want to see if the * character ever shows up on a line where it is the only character on the line. as seen below. so lets say we have a array with...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing...
C Programming Language (Code With C Programming Language) Problem Title : Which Pawn? Jojo is playing chess himself to practice his abilities. The chess that Jojo played was N × N. When Jojo was practicing, Jojo suddenly saw a position on his chessboard that was so interesting that Jojo tried to put the pieces of Rook, Bishop and Knight in that position. Every time he put a piece, Jojo counts how many other pieces on the chessboard can be captured...
C# programming. Comment/Explain the below code line by line. I am having a hard time following...
C# programming. Comment/Explain the below code line by line. I am having a hard time following it. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Nth_prime {     class Program     {         public static bool isPrime(int number)         {             int counter = 0;             for (int j = 2; j < number; j++)             {                 if (number % j == 0)                 {                     counter = 1;                     break;                 }             }             if (counter == 0)             {                 return true;             }             else             {                 return false;             }         }...
Assembly Language Programming Construct an assembly language program fragment equivalent to the following C/C++ statement: if...
Assembly Language Programming Construct an assembly language program fragment equivalent to the following C/C++ statement: if (M <= N + 3 && (C == ‘N’ || C == ‘n’)) C = ‘0’; else C = ‘1’; Assume that M and N are 32-bit signed integer variables, and C is an 8-bit ASCII character variable. All variables are stored in memory, and all general-purpose registers are available for use.
IN PROGRAMMING LANGUAGE C -I am trying to alphbetize a string in descending or to EX...
IN PROGRAMMING LANGUAGE C -I am trying to alphbetize a string in descending or to EX INPUT: B C D A OUTPUT: D C B A #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char*argv[]) {         int MAX = 100000;         int i =0;         int k =0;         int j =0;         char array[MAX];         char split[] = " ,.-!?()0123456789";         int n = 0;         char second[MAX];         printf("Please enter in a String: ");...
I am building a game in C programming language where I need to add objects of...
I am building a game in C programming language where I need to add objects of various length into a game board. The game board is 8X8 and we must account for the boundaries for the board and not go over them with our objects. The boards upper left corner is at 0x0 and we must return 1 if it fits and -1 if it does not fit. I have the following 2 functions to start with: ```int add_object_vert(int r,...
I have a question about C++ programming Language class. I am confused with these terms and...
I have a question about C++ programming Language class. I am confused with these terms and what they are for. 1. Unix 2. Terminal 3. Git 4. CLOC 5. Linux Please explain each words and what's their jobs for C++. Also, if you know some good sources/websites, could you please provide me a link where I can learn how to use Unix, Terminal, Git, etc.
Hi, I am working on an assignment in C-Programming language dealing with LInked lists, in the...
Hi, I am working on an assignment in C-Programming language dealing with LInked lists, in the code there is instructions on where to write the code. I do not know how to write Linked Lists. Has to be in the C-language, Any help is greatly appreciated   //agelink.c //maintains list of agents //uses linked list #include <stdio.h> #include <stdlib.h> #define TRUE 1 void listall(void); void newname(void); void delink(void); void memexit(void); void wfile(void); /********************************************************************* this is the structure to hold a agent...
The following question must be answered in the C programming language and may not be written...
The following question must be answered in the C programming language and may not be written in C++ or any other variation. Problem 5 This problem is designed to make sure you can write a program that swaps data passed into it such that the caller's data has been swapped. This is something that is done very frequently in manipulating Data Structures. The Solution / Test Requirements I want your program to demonstrate that you understand how to swap information...
C programming language. **I am aware that I am only supposed to ask one question so...
C programming language. **I am aware that I am only supposed to ask one question so if you cant do all of this could you please do part 2? thank you! This lab, along with your TA, will help you navigate through applying iterative statements in C. Once again we will take a modular approach to designing solutions to the problem below. As part of the lab you will need to decide which C selection structure and iterative structure is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT