C Programming - File io parsing strings using sscanff

I am trying to do the following the C programming language, any help or if you can finish the code I will be greatly appreciated:

I am trying to write a program in C programming language that uses file io, that will parse through the words using sscanf function and output each word in all the sentences inside a txt document (bar.txt). Here is the instructions.

Write a program that opens the file bar.txt name the program "reader". Pass a parameter to indicate lines to read. Read all the lines in the file based on the parameter 'lines' into a buffer and using sscanf parse all the words of the sentences into different string* variables. Print each of the words to the screen followed by a carriage return. You can hardwire filename (path of bar.xt) or use option to enter filename.

This is the txt file (bar.txt) i am working with:

****bar.txt****

this is the first sentence
this is the 2nd sentence
this is the 3rd sentence
this is the 4th sentence
this is the 5th sentence

****end of file: bar.txt****

usage of argv: Usage: updater [-f "filename"] 'lines'

-f is optional (if not provided have a hardwired name from previous program 2 (bar.txt))
'lines' integer from 1 to 10 (remember the files has 5-10 strings from previous program)
a sample input example for the input into the program is:

./reader -f bar.txt 1

OUTPUT:

Opening file "bar.txt"

File Sentence 1 word 1 = this

File Sentence 1 word 2 = is

File Sentence 1 word 3 = the

File Sentence 1 word 4 = first

File Sentence 1 word 5 = sentence

***another example***

./reader -f bar.txt 5

OUTPUT:

File Sentence 5 word 1 = this

File Sentence 5 word 2 = is

File Sentence 5 word 3 = the

File Sentence 5 word 4 = 5th

File Sentence 5 word 5 = sentence

Examples of commands:

./reader -f bar.txt 5

./reader -f bar.txt 2

./reader -f bar.txt 7

./reader 2

./reader 5

./reader 8

./reader 11

this is the code that I have so far please fix the code to show the desired output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdlib.h>

#include <stdio.h>

#define MAXCHAR 1000

int main(int argc, char *argv[]) {

   FILE *file;

    char string[MAXCHAR];

    char* filename = "c:\\cprogram\\fileio-activity\\bar.txt";

    int integer = argv[3][0] - 48;

    int i; //for loops

if (argv[1][0] == '-' && argv[1][1] == 'f')

   {

    file = fopen(filename, "r");

    if (file == NULL){

        printf("Could not open file %s",filename);

        return 1;

    }

    while (fgets(string, MAXCHAR, file) != NULL)

        printf("%s", string);

    fclose(file);

    return 0;

}

}
Hello alexexcl,

You are getting ahead of your-self.

First you need to deal with "argc" then "argv".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#define MAXCHAR 1000

const char* argv[]{ "", "-f", "bar.txt", "2" }; // <--- Used for testing. Comment or remove when finished.
const int argc{ 2 }; // <--- Used for testing. Comment or remove when finished.

int main(/*int argc, char *argv[]*/)
{
    if (argc < 3)
    {
        printf("\n     Wrong number of parameters!\n       usage is Program Name [-f] File Name number of lines to print\n"
               "          (Example) Main V1.exe [-f] bar.txt 5\n\n");

        return 1;
    }

    return 0;
}

Feel free to change the error message as you like. This is just an idea.

Lines 3 and 4 just make it easier to test the program. You could copy lines 3 and 4 and change them around to test different choices. You just have to switch comments to try something different.

Following this if statement you need to check if "argc" is 3 or 4 to know where the file name is.

You may also want to cheek the switch and using "tolower()" from the "ctype.h" header file change it to lower case if needed. Otherwise you should make 2 checks 1 for lower case and 1 for upper case.

When you have figured out where the file name is you can copy it from "argv[?]" to "filename". If there is a path you could define the path and add "argv[?]" before putting the whole into "filename".

Then you can continue on to deal with the last "argv" then open the file.

I would start with this first because you need to deal with the command line arguments first before you can continue with the program.

That will give me a chance to check the rest of the program.

Andy
Hello alexexcl,

The subject refers to using "sscanff". Did you mena "sscanf"?

The next question is why do you want to use formatted input to read a whole line. This does not work. After discarding any leading white space formatted input will read until it finds a white space or "\n" whichever comes first.

The "fgets" that you started with works just fine.

Andy
Last edited on
Once you have sorted out parsing the arguments, then a possible way to parse the file is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
	const int lno = 5;	// line number to use

	FILE* infile = fopen("bar.txt", "r");

	if (infile == NULL) {
		puts("Cannot open file");
		return 1;
	}

	char line[256] {};

	for (int cl = 0; cl < lno && fgets(line, 255, infile) != NULL; ++cl);

	int wno = 1;

	for (char* word = line, *fnd = line; (fnd = strpbrk(word, " \n")) != NULL; word = fnd + 1, ++wno) {
		*fnd = 0;
		printf("File sentence %d Word %d = %s\n", lno, wno, word);
	}
}


However, this uses strpbrk() to split the string into words and not sscanf() - but is a more general solution.

The questions asks "using sscanf parse all the words of the sentences into different string* variables"

To parse into different string variables seems to imply that the number is known. The given example file has 5 words per line. Assuming 5 words per line and parsing the required line into 5 different string variables using sscanf() could be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
	const int lno = 5;	// line number to use

	FILE* infile = fopen("bar.txt", "r");

	if (infile == NULL) {
		puts("Cannot open file");
		return 1;
	}

	char line[256] {};

	for (int cl = 0; cl < lno && fgets(line, 255, infile) != NULL; ++cl);

	char word1[256] {};
	char word2[256] {};
	char word3[256] {};
	char word4[256] {};
	char word5[256] {};

	char* wrds[] = {word1, word2, word3, word4, word5};

	sscanf(line, "%255s%255s%255s%255s%255s", word1, word2, word3, word4, word5);

	for (int w = 0; w < 5; ++w)
		printf("File sentence %d Word %d = %s\n", lno, w + 1, wrds[w]);
}

Topic archived. No new replies allowed.