The purpose of the program is to read a text file that contains a list of 55 authors and titles of books. The format of the list goes (author name, booktitle). I can use malloc, strlen, strtok, and strcopy. So far I got the program to read out the names of the authors but I am stuck on how to get the program to read the titles of the books.How would I get the program to read the titles of the books from the text file? I know that there are errors in this code so please be kind .
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void loadBookName(char* filename, char* authorName[55], char* bookName[55]);
int main(int argc, char* argv[])
{
//Create two arrays each with length 55
char* authorName[55];
char* bookName[55];
//Ask the user for the name of the file
char fileName[30];
//Insert your code here
printf("Please enter the name of the file\n");
scanf("%s", fileName);
//Call the method loadBookName
loadBookName(fileName, authorName, bookName);
return 0;
//Print the two arrays to test if the two arrays were correctly loaded with the data
int i = 0;
printf("%-30s%-40s\n", "Author", "Book");
for (i = 0; i < 55; i++) {
printf("%-30s%-40s\n", authorName[i], bookName[i]);
}
}
/*
loadBookName method
This method is responsible for:
1. Take a file containing a book name and the author name as input
2. Open the file
3. Read the information in the file and store it in two arrays: authorName, bookName
4. Return the two arrays to the main method.
*/
void loadBookName(char* filename, char* authorName[55], char* bookName[55])
{
int i;
char string_array[80];
const char comma[2] = ",";
//Open the file
FILE *fp;
fp = fopen(filename, "r");
if (fp == NULL)
{
printf("Failed to open file\n");
exit(1);
}
for (i=0; i<55; i++)
{
fgets(string_array, 80, fp);
authorName[i] = strtok(string_array, comma);
printf("%s\n", *authorName);
}
//Close the file
fclose(fp);
}
when I run the program in terminal it asks me to enter the filename (books.txt). Then when I enter the file name, the program prints a list of 55 authors.
I don't have a compiler in front of me, so excuse the compilation error if it has any. But I think you can try something as below, within your existing code:
UPDATED:
After comments, I've updated one line. This is compiled and working.
Assumptions: User need to take care of error-handling e.g. file not present, file unable to open, buffer overflows etc.
char *token;
for (i=0; i<55; i++)
{
//fgets(string_array, 80, fp);
//This will take care in case if lines are less than 55
if(!fgets(string_array, 80, fp))
break;
//Get the author
token = strtokstring_array, comma);
authorName[i] = token; // or use string copy functions
//Get book name
while( token != NULL )
{
printf( " %s\n", token ); //this shall print author name
token = strtok(NULL, comma);
bookName[i] = token;
printf( " %s\n", token ); //this shall print book name
//EDIT: This is additional line after suggestions
token = strtok(NULL, comma);
}
}
Simple way of separating strings with strlcpy:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(void )
{
size_t i = 1;
char *authorName, *bookName;
const char *a_line_in_a_file =
"Lewis Carroll,The Hunting of the Snark";
const char *title = a_line_in_a_file;
while ( *title != ','){
title++;
i++;}
authorName = malloc(i);
bookName = malloc(strlen(title));
title++;
#if __BSD_VISIBLE
strlcpy(bookName, title, strlen(title) + 1);
strlcpy(authorName, a_line_in_a_file, i);
#else
snprintf(bookName, strlen(title) + 1, "%s", title);
snprintf(authorName, i, "%s", a_line_in_a_file);
#endif
printf("%-30s%-40s\n", authorName, bookName);
free(authorName);
free(bookName);
return 0;
}
Related
Suppose I have a text file such as:
Adam: Tall Handsome Kind Athlete
He enjoys playing basketball
Sabrina: Short Pretty Funny Adorable Gymnast
She loves gymnastics
Sinclair: Blonde
He is blonde
Assume the file has several more people, each with a Name and then 0 or more characteristics about them
and then a new line with a tab following a sentence underneath.
For example,
Adam: would be the name
Tall Handsome Kind Athlete would be 4 individual characteristics
He enjoys playing basketball would be the sentence.
I want to store this information in a structure like so:
typedef struct People {
char *name;
char **characteristics;
char *sentence;
} People;
typedef struct List {
People **list;
int total_ppl;
} List;
int main (void) {
List *ppl_list = malloc(sizeof(List));
ppl_list->list = malloc(sizeof(People));
int i = 0;
FILE *pf = fopen("input.txt", "r");
if (pf == NULL) {
printf("Unable to open the file");
} else {
/* I'm not sure how to go about reading the information from here. I was thinking about using
fscanf but I don't know how to separate and store the Name, each Characteristic, and
the sentence separately. I know I will need to realloc ppl_list as more people are read from the
file. If I should change my structs to organize it better, please let me know.
*/
}
}
This answer is maybe not complete but it will help you, at least, with the processing of the lines in the text file.
Assuming a file.txt as the input file, and with the following format
Adam: Tall Handsome Kind Athlete
He enjoys playing basketball
Sabrina: Short Pretty Funny Adorable Gymnast
She loves gymnastics
Sinclair: Blonde
He is blonde
We can process this file as follows
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*process_info_line: process the line with the name and attributes*/
int process_info_line(char *line)
{
char *next = NULL;
char *part = strtok_r(line, ":", &next);
if (part)
printf("NAME: %s\n", part);
else
return 0;
while (part != NULL) {
part = strtok_r(NULL, " ", &next);
if (part)
printf("ATTRIBUTE: %s\n", part);
}
return 0;
}
/*process_sentence: process the line with the sentence*/
char *process_sentence(char *line)
{
line = line + 4;
return line;
}
/*is_sentence: checks if the line is a sentence, given your definition
* with a tab(or 4 spaces) at the begining*/
int is_sentence(char *line)
{
if (strlen(line) == 0)
return 0;
char *ptr = line;
int space_count = 0;
while (ptr != NULL) {
if (strncasecmp(ptr, " ", 1) != 0) {
break;
}
space_count++;
ptr++;
}
if (space_count == 4)
return 1;
return 0;
}
/*scan_file: read each of the lines of the file and use
* the previous functions to process it.*/
int scan_file(char *filename)
{
char *line_buf = NULL;
size_t line_buf_size = 0;
ssize_t line_size;
int line_count = 0;
FILE *fp = fopen(filename, "r");
if (!fp) {
fprintf(stderr, "Error opening file '%s'\n", filename);
return 1;
}
/*Get the first line of the file*/
line_size = getline(&line_buf, &line_buf_size, fp);
while (line_size >= 0)
{
line_count++;
line_buf[line_size-1] = '\0'; /*removing '\n' */
if (is_sentence(line_buf)) {
printf("SENTENCE: %s\n", process_sentence(line_buf));
} else {
process_info_line(line_buf);
}
line_size = getline(&line_buf, &line_buf_size,fp);
}
// don't forget to free the line_buf used by getline
free(line_buf);
line_buf = NULL;
fclose(fp);
return 0;
}
int main(void)
{
scan_file("file.txt");
return 0;
}
This will generate the following output
NAME: Adam
ATTRIBUTE: Tall
ATTRIBUTE: Handsome
ATTRIBUTE: Kind
ATTRIBUTE: Athlete
SENTENCE: He enjoys playing basketball
NAME: Sabrina
ATTRIBUTE: Short
ATTRIBUTE: Pretty
ATTRIBUTE: Funny
ATTRIBUTE: Adorable
ATTRIBUTE: Gymnast
SENTENCE: She loves gymnastics
NAME: Sinclair
ATTRIBUTE: Blonde
SENTENCE: He is blonde
There is a function called strtok(): https://www.tutorialspoint.com/c_standard_library/c_function_strtok.htm
Though I've never used it, the one time I had to do this I implemented a function that would separate a string into an array of pointers to pointers of chars and dynamically allocated memory for the whole block, it would look for commas and generate a new string each time it found one, my code wouldn't work in your case because it was written to specifically ignore withe spaces, though if you're not ignoring them but using them as delimitators the code gets a lot simpler.
Edit: as for getting the info out of the file I would create a buffer of an absurd size like say 32768 Bytes and use fgets(buffer, 32768, pf), though you may wanna add a check to see if even 32K chars weren't enough for the read and to deal with that, though I imagine it wouldn't be necessary.
Also this were the prototypes of the functions i implemented once, to give you a better idea of what you'd have to code:
char **separete_string (char *string, char delim);
void free_string_list (char **list);
I am currently learning about to how to data structures in C and I need a little help. I am supposed to take information about classes from a .txt file and store the information in a data structure; but I am having trouble doing so. I am also sure that I am also screwing up a lot of other things in my program, so feel free to bash on my program and tell me what I am doing wrong so I can learn from my mistakes.
Here is one line of information that I am trying to store:
M273 Multivariable Calculus :MWF 0900-0950 2
where the first part is the course number, the second part is the course name, the third part is the days and time the course is available and the last number represents what year you should be in to take the course (2 translates to sophomore).
Below is my code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define EMAX 250
typedef struct class{
char *classNumber[EMAX];
char *className[EMAX];
char *classTime[EMAX];
char *classStanding[EMAX];
}class;
void menu(class*info, char buffer[], FILE*file);
void setInformation(class*info, char buffer[], FILE*file);
int main(void)
{
class info[EMAX];
char buffer[EMAX];
File *file;
setInformation(info, buffer, file);
menu(info, buffer, file);
return(0);
}
void menu(class*info, char buffer[], FILE*file)
{
int user_input=0;
do {
printf("\nSelect one of the following options: \n");
printf("1) Print all information about all classes in order of the class number\n");
printf("5) Quit\n");
scanf("%d", &user_input);
if(user_input==1)
{
//getInformation(info, buffer, file);
}
}while(user_input!=5);
}
void setInformation(class*info, char buffer[], FILE*file)
{
size_t count = 0;
char line[50];
char *token;
file = fopen("classes.txt", "r");
while(fgets(line, sizeof(line), file)!=NULL)
{
token=strtok(line, " "); //Only gets the course number
strncpy(info[count].classNumber, token, strlen(token));
count++;
}
fclose(file);
}
As you can tell, I can only extract the course number with this code. I would prefer to store all the data in one while loop and I have tried to extract more information by adding another token to stop as soon as it reaches the ":" before the day and time but I can't figure out how to get it to work. I also get a lot of warnings when I compile this, so I welcome any advice to help my trash code. I appreciate any help
*scanf() is great:
#include <stdlib.h>
#include <stdio.h>
#define EMAX 250
#define STRING(X) #X
#define STRINGIFY(X) STRING(X)
typedef struct class_tag {
char classNumber[EMAX + 1];
char className[EMAX + 1];
char classTime[EMAX + 1];
char classStanding[EMAX + 1];
char foo[EMAX + 1];
} class;
int main(void)
{
char const *input_filename = "test.txt";
FILE *input = fopen(input_filename, "r");
if (!input) {
fprintf(stderr, "Couldn't open \"%s\" for reading :(\n\n", input_filename);
return EXIT_FAILURE;
}
class c;
class *classes = NULL;
size_t classes_size = 0;
while (fscanf(input, "%"STRINGIFY(EMAX)"s %"STRINGIFY(EMAX)"[^:] %"STRINGIFY(EMAX)"s "
"%"STRINGIFY(EMAX)"s %"STRINGIFY(EMAX)"s",
c.classNumber, c.className, c.classTime, c.classStanding, c.foo) == 5)
{
class *tmp = realloc(classes, ++classes_size * sizeof(*classes));
if (!tmp) {
fputs("Not enough memory :(\n\n", stderr);
fclose(input);
free(classes);
return EXIT_FAILURE;
}
classes = tmp;
classes[classes_size - 1] = c;
}
fclose(input);
for (size_t i = 0; i < classes_size; ++i)
printf("%s %s %s %s\n", classes[i].classNumber, classes[i].className, classes[i].classTime, classes[i].classStanding);
free(classes);
}
I'm trying to get two words in a string and I don't know how I can do it. I tried but if in a text file I have 'name Penny Marie' it gives me :name Penny. How can I get Penny Marie in s1? Thank you
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello world!\n");
char s[50];
char s1[20];
FILE* fp = fopen("file.txt", "rt");
if (fp == NULL)
return 0;
fscanf(fp,"%s %s",s,s1);
{
printf("%s\n",s);
printf("%s",s1);
}
fclose(fp);
return 0;
}
Change the fscanf format, just tell it to not stop reading until new line:
fscanf(fp,"%s %[^\n]s",s,s1);
You shall use fgets.
Or you can try to do this :
fscanf(fp,"%s %s %s", s0, s, s1);
{
printf("%s\n",s);
printf("%s",s1);
}
and declare s0 as a void*
The other answers address adjustments to your fscanf call specific to your stated need. (Although fscanf() is not generally the best way to do what you are asking.) Your question is specific about getting 2 words, Penny & Marie, from a line in a file that contains: name Penny Marie. And as asked in comments, what if the file contains more than 1 line that needs to be parsed, or the name strings contain a variable number of names. Generally, the following functions and techniques are more suitable and are more commonly used to read content from a file and parse its content into strings:
fopen() and its arguments.
fgets()
strtok() (or strtok_r())
How to determine count of lines in a file (useful for creating an array of strings)
How to read lines of file into array of strings.
Deploying these techniques and functions can be adapted in many ways to parse content from files. To illustrate, a small example using these techniques is implemented below that will handle your stated needs, including multiple lines per file and variable numbers of names in each line.
Given File: names.txt in local directory:
name Penny Marie
name Jerry Smith
name Anthony James
name William Begoin
name Billy Jay Smith
name Jill Garner
name Cyndi Elm
name Bill Jones
name Ella Fitz Bella Jay
name Jerry
The following reads a file to characterize its contents in terms of number of lines, and longest line, creates an array of strings then populates each string in the array with names in the file, regardless the number of parts of the name.
int main(void)
{
// get count of lines in file:
int longest=0, i;
int count = count_of_lines(".\\names.txt", &longest);
// create array of strings with information from above
char names[count][longest+2]; // +2 - newline and NULL
char temp[longest+2];
char *tok;
FILE *fp = fopen(".\\names.txt", "r");
if(fp)
{
for(i=0;i<count;i++)
{
if(fgets(temp, longest+2, fp))// read next line
{
tok = strtok(temp, " \n"); // throw away "name" and space
if(tok)
{
tok = strtok(NULL, " \n");//capture first name of line.
if(tok)
{
strcpy(names[i], tok); // write first name element to string.
tok = strtok(NULL, " \n");
while(tok) // continue until all name elements in line are read
{ //concatenate remaining name elements
strcat(names[i], " ");// add space between name elements
strcat(names[i], tok);// next name element
tok = strtok(NULL, " \n");
}
}
}
}
}
}
return 0;
}
// returns count, and passes back longest
int count_of_lines(char *filename, int *longest)
{
int count = 0;
int len=0, lenKeep=0;
int c;
FILE *fp = fopen(filename, "r");
if(fp)
{
c = getc(fp);
while(c != EOF)
{
if(c != '\n')
{
len++;
}
else
{
lenKeep = (len < lenKeep) ? lenKeep : len;
len = 0;
count++;
}
c = getc(fp);
}
fclose(fp);
*longest = lenKeep;
}
return count;
}
Change your fscanf line to fscanf(fp, "%s %s %s", s, s1, s2).
Then you can printf your s1 and s2 variables to get "Penny" and "Marie".
Try the function fgets
fp = fopen("file.txt" , "r");
if(fp == NULL) {
perror("Error opening file");
return(-1);
}
if( fgets (str, 60, fp)!=NULL ) {
/* writing content to stdout */
puts(str);
}
fclose(fp);
In the above piece of code it will write out the content with the maximum of 60 characters. You can make that part dynamic with str(len) if I'm not mistaken.
My task is to find word palindromes in a text file and to NOT print them into results file. The results file should only contain all the spaces and words that are NOT palindromes. I've been working on this program for two solid weeks, but as I am a total newb in C, I can't simply imagine how to do this correctly. Also, I have to work in Linux environent, so I can't use commands like strrev() which would make my life a lot easier at this point...
Anyways, data file contains a lot of words in a lot of lines separated by quite a few spaces.
Here is the program that is working, but doesn't work with any spaces, because I don't know how to check them at the needed place.
#include <stdio.h>
#include <string.h>
const int CMAX = 1000;
const int Dydis = 256;
FILE *dataFile;
FILE *resFile;
void palindrome(char *linex);
int main(){
char duom[CMAX], res[CMAX], linex[Dydis];
printf("What's the name of data file? \n");
scanf("%s", duom);
dataFile=fopen(duom, "r");
if (dataFile==NULL){
printf ("Error opening data file \n");
return 0;
};
printf("What's the name of results file? \n");
scanf ("%s", res);
resFile=fopen(res, "w");
if (resFile==NULL){
printf ("Error opening results file \n");
return 0;
};
while (fgets(linex, sizeof(linex), dataFile)) {
palindrome(linex);
}
printf ("all done!");
fclose(dataFile);
fclose(resFile);
}
void palindrome(char *linex){
int i, wordlenght, j;
j = 0;
char *wordie;
const char space[2] = " ";
wordie = strtok(linex, space);
while ( wordie != NULL ) {
wordlenght = strlen(wordie);
if (wordie[j] == wordie[wordlenght-1]) {
for (i = 0; i < strlen(wordie); i++) {
if (wordie[i] == wordie[wordlenght-1]) {
if (i == strlen(wordie)-1) {
fprintf(resFile,"");
}
wordlenght--;
}
else {
fprintf(resFile,"%s", wordie);
break;
}
}
}
else {
fprintf(resFile,"%s", wordie);
}
wordie = strtok(NULL, space);
}
}
EDIT:
Code below works as following:
input file is read char by char
if char read isn't alphanumeric, then it is written to the output file
else, the whole word is read with fscanf
if word is not a palindrome, then write to the output file
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int is_pal(char* word) {
size_t len = strlen(word);
char* begin = word;
char* end = word + len - 1;
if (len == 1) {
return 1;
}
while (begin <= end) {
if (*begin != *end) {
return 0;
}
begin++;
end--;
}
return 1;
}
int main(void)
{
FILE* fin = fopen("pals.txt", "r");
if (fin == NULL) {
perror("fopen");
exit(1);
}
FILE* fout = fopen("out_pals.txt", "w");
if (fout == NULL) {
perror("fopen");
exit(1);
}
int ret;
char word[100];
while ((ret = fgetc(fin)) != EOF) {
if (!isalpha(ret)) {
fprintf(fout, "%c", ret);
}
else {
ungetc(ret, fin);
fscanf(fin, "%s", word);
if (!is_pal(word)) {
fprintf(fout, "%s", word);
}
}
}
fclose(fin);
fclose(fout);
return 0;
}
I've created file with following content:
cancer kajak anna sam truck
test1 abc abdcgf groove void
xyz annabelle ponton belowoleb thing
cooc ringnir
The output file :
cancer sam truck
test1 abc abdcgf groove void
xyz annabelle ponton thing
(line with two spaces)
As you can see, the number of spaces between words are the same as in the input file.
I've assumed that single word could have 100 chars maximum. If there would be longer words, reading with fscanf onto fixed-size buffer can be harmful.
Hints:
strtok() gives you a pointer to the start of delimited words but it does not
extract them or put them in their own string for you.
You need some logic to find the end of each word. The function
strlen() will tell you how many characters there are from the char*
that it gets until a null-character. If you give it a pointer to the start
of a word within a sentence it will give you the length from the start of the
word to the end of the sentence.
Breaking palindrome() into a function that loops over words in a line and a
function that returns whether or not a single word is a palindrome
may help.
Your for loop is checking each pair of letters twice. i only needs to scan over half
of the word length.
You only need a single if within palindrome(). I'm not sure why you have so many.
They're redundant.
So, basically this code below need the user to login first, then after the user login it will show the user details like the one stored in the user.txt. after that i dunno how to retrieve back the files from the files then return it as an array, so that i can update e.g change the name of the user, or delete the user details by taking the array index
here is my code
#include <stdio.h>
#include <string.h>
typedef struct {
char fullname[30];
char dob [10];
int contactNo;
int postcode;
}userDetails;
int main ()
{
char username [15];
char pwd [20];
char user_pass [30];
char userfile [100];
FILE *user;
FILE *admin;
userDetails myUser;
admin = fopen ("admin.txt","r");
printf("Please enter your Username\n");
scanf("%s",username);
printf("Please enter your Password\n");
scanf("%s",pwd);
user_pass[strlen(username) + strlen(pwd) + 2];
sprintf(user_pass, "%s;%s", username, pwd);
while (fgets(userfile, 100, admin) != NULL) {
if (strcmp(userfile, user_pass) == 0) {
printf("Authentication as %s successful\n", username);
size_t nread; // Printing the user information
user = fopen("user.txt", "r");
printf("\nHere is the registered user:\n");
if (user) {
while ((nread = fread(myUser.fullname, 1, sizeof myUser.fullname, user)) > 0)
fwrite(myUser.fullname, 1, nread, stdout);
if (ferror(user)) {
}
fclose(user);
}
}
else{
printf("Please enter correct username and password\n");
}
}
}
and let say in the user.txt the file is stored in a format like this
john;12/12/1990;+6017012682;57115
paul;12/12/1221;+60190002122;100022
max;12/11/1990;+60198454430;900000
jamie;12/05/2000;+60190001231;18000
Thank you
how do you read a data from a files and store it to an array, then read specific stored data in the array to be edited?
Steps to store data:
1) - Declare the type(s) of storage variables needed to support your concept. Array of struct perhaps.
2) - Open file ( FILE *fp = fopen("file path", "r"); )
3) - Loop on fgets() to read each line (record) of file
4) - Parse lines, perhaps using strtok and place elements of each line into storage variable you created above.
5) - close file ( fclose(fp); )
The get record part can be done by selecting a record, and returning a re-concatenated string composed of each field of the original record. The prototype could look like this:
void GetRecord(RECORD *pRec, int rec, char *recordStr);
Where storage would be created as an array of struct, and the struct would accommodate the 4 fields you cited, eg:
John;12/12/1990;+6017012682;57115 //example record with 4 fields
#define MAX_RECORDS 10 //arbitrary value, change as needed
typdef struct {
char name[260];
char date[11];
char num1;
char num2;
} RECORD;
RECORD record[MAX_RECORDS];//change size to what you need, 10 is arbitrary for illustration
Code Example to read data into records , and retrieve a record, could look like this:
(example only, very little error checking)
void GetRecord(RECORD *pRec, int rec, char *record);
int main(void)
{
FILE *fp = {0};
char recStr[260];
char *buf;
char line[260];
int i;
char strArray[3][7];//simple array, 3 string of 7 char each (enough for NULL terminator)
i = -1;
fp = fopen ("admin.txt", "r");
if(fp)
{
while (fgets (line, 260, fp))//will continue to loop as long as there is a new line
{
i++;
if(i >= MAX_RECORDS) break;//reached maximum records defined, time to leave
buf = strtok(line, ";");
if(buf)
{
strcpy(record[i].name, buf);
buf = strtok(NULL, ";");
if(buf)
{
strcpy(record[i].date, buf);
buf = strtok(NULL, ";");
if(buf)
{
record[i].num1 = atoi(buf);
buf = strtok(NULL, ";");
if(buf)
{
record[i].num2 = atoi(buf);
}
}
}
}
}
}
fclose(fp);
// 2nd argument valid values range from 1 - n (not 0 - n)
GetRecord(record, 3, recStr); //read third record into string "rec"
return 0;
}
void GetRecord(RECORD *pRec, int rec, char *record)
{
int r = rec - 1;//adjust for zero indexed arrays
if((r >= MAX_RECORDS) || (r < 0))
{
strcpy(record, "index error");
return;
}
sprintf(record, "%s;%s;%d;%d", pRec[r].name, pRec[r].date, pRec[r].num1, pRec[r].num2);
}
Note: "coz [you were] rushing and just copy the code, and accidentaly delete the important things",
I have not adhered strictly to your variable names. Adjust what I have done to meet your needs.
Results with your input file look like this: