Question

C Programming Files.. A file has lines of text and on each there are 3 integers....

C Programming Files..

A file has lines of text and on each there are 3 integers. How to extract each of them from all lines and store in a linked list?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*Source code of the above said program is given below*/

/*NOTE: If you want to change something , please let me know through comments; I will surely revert back to you.*/

/*Assumption: input file name is given here as inputData.txt; the file contents multiple lines and each line contains 3 integers and file is ended with a new blank line.*/

#include <stdio.h>

#include <stdlib.h>

// A linked list node

typedef struct Node

{

int data;

struct Node *next;

}node;

/*Create a node,returns a pointer to that node*/

node *makeNode(int val)

{

/* allocate node */

node* newNode = (node*) malloc(sizeof(node));

/* put in the data */

newNode->data = val;

/*set next as NULL*/

newNode->next = NULL;

  

return newNode;

}

/*Inserts a new node at the end of the list and return head*/

node *insert(node *head, node *newNode)

{

//declare a temp node

node* temp=head;

/* Make next of new node NULL */

newNode->next = NULL;

/* If the Linked List is empty, then make the new node as head */

if(head==NULL)

{

head=newNode;

return head;

}

/*Else move a temp pointer to point to the last node */

while (temp->next != NULL)

temp = temp->next;

/* Change the next of last node */

temp->next = newNode;

/*Return head*/

return head;

}

// Prints contents of linked list starting from head

void printList(node *head)

{

while (head != NULL)

{

printf("%d ", head->data);

head = head->next;

}

}

int main()

{

/*Declaring necessary variables*/

int i;

node *head=NULL;

node *newNode;

//Declare and initialize a filename variable

char file_name[]="inputData.txt";//input file

//Opening input file

FILE* file = fopen (file_name, "r");

//Checking if file can be opened successfully

if(!file)

{

printf("Input File named %s cannot be opened",file_name);

exit(1);

}  

//Reading integer data from file

fscanf (file, "%d", &i);   

while (!feof (file))

{  

//Creating a new node with value of i

newNode=makeNode(i);

//Inserting new node into link list

head=insert(head,newNode);

//Reading data from file

fscanf (file, "%d", &i);

}

//Closing file

fclose (file);

printf("\nContents of the linklist:\n");

/*Display list*/

printList(head);

return 0;

}

/*For better understanding output screenshot and input file image are attached here*/

OUTPUT:

С:\Users\AmartyalDesktop\RumaC\FileReadInLL.exe Contents of the linklist: 11 2 34 21 99 19 3 42 28 46 7 91 Process exited aft

Input File:

inputData Notepad File Edit Format View Help 11 2 34 21 99 19 3 42 28 46 7 91

/*Hope this will help you.*/

/*If this helps you, please let me know by giving a positive thumbs up. In case you have any queries, do let me know. I will revert back to you. Thank you!!*/

Add a comment
Know the answer?
Add Answer to:
C Programming Files.. A file has lines of text and on each there are 3 integers....
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
  • Write c program. Do part 4 and 5 CH-12 TEXT FILES SE 12-3 Create a text file named grade.txt that you type yourself by your Each record in grade.txt should have the following format: by Columns...

    Write c program. Do part 4 and 5 CH-12 TEXT FILES SE 12-3 Create a text file named grade.txt that you type yourself by your Each record in grade.txt should have the following format: by Columns 1-4 6-8 number of a student A grade out of 100 EYERCISE 12-4 Write a program that will read the identification numbers of students and their letter grades (A, B, C, D, or F) from the keyboard and store them in a text file...

  • IN PYTHON Write a program that reads two text files, take one sentence from each text...

    IN PYTHON Write a program that reads two text files, take one sentence from each text file and print them one after another. Example: If line1 is from text1.txt and line2 is from text2.txt, then print line1 line2 Repeat this for all the lines of the two files

  • 3. Write a program to read a (binary) file of integers, sort the integers, and write...

    3. Write a program to read a (binary) file of integers, sort the integers, and write them back to the same file. Assume that all the numbers can be stored in an array. (Exercise 3) 4. Repeat exercise 3, but assume that only 20 numbers can be stored in memory (in an array) at any one time. Hint: you will need to use at least two additional files for temporary output. Please finish the question in C language programming, thank...

  • Description: Merge two or more text files provided by the user into a new text file...

    Description: Merge two or more text files provided by the user into a new text file where the names of the text files to join and the output text file are provided to the program using command line arguments. Purpose: This application provides experience working with command line arguments, writing files to disk, reading files from disk, working with exceptions, and writing programs in C#/.NET. Requirements: Project Name: DocumentMerger2 Target Platform: Console Programming Language: C# In a previous challenge, Document...

  • C Programming: I have a text file which has this on it 1 2 3 4...

    C Programming: I have a text file which has this on it 1 2 3 4 5 6 I want my output to display 123456 How do I do this?

  • Write a program that allows the user to navigate the lines of text in a file....

    Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program...

  • All the white space among words in a text file was lost. Write a C++ program...

    All the white space among words in a text file was lost. Write a C++ program which using dynamic programming to get all of the possible original text files (i.e. with white spaces between words) and rank them in order of likelihood with the best possible runtime. You have a text file of dictionary words and the popularity class of the word (words are listed from popularity 1-100 (being most popular words), 101-200, etc) - Input is a text file...

  • Python programming with file handling. File.csv contains a list of numbers, one integer on each line....

    Python programming with file handling. File.csv contains a list of numbers, one integer on each line. Calculate the average of every 10 lines of integers and write them into another file. For example, the first 10 lines that is line 1 to line 10 contains 61, 70, 71, 90, 81, 82, 85, 90, 61, 51 each on every line

  • Write a complete Python program with prompts for the user for the main text file (checks...

    Write a complete Python program with prompts for the user for the main text file (checks that it exists, and if not, output an error message and stop), for any possible flags (including none), and for any other input that this program may need from the user: split has an option of naming the smaller files head_tail list the first 10 lines (default) and the last 10 lines (default) in order of the given text file flag: -# output #...

  • Programming language --> Matlab The name of a text file Outputs: (cell) An Nx2 cell array...

    Programming language --> Matlab The name of a text file Outputs: (cell) An Nx2 cell array listing words and their counts in sorted order Function Description: Have you ever seen those fancy word-clouds based on the frequency of word occurrences in some text? Well the first step of making a graphic like that is to figure out how often the words in some text occur, which is exactly what this function will do. You will count the number of occurrences...

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