I got this code from a question and i don't understand why they have picked exactly 300 for line[300] is it because a line of .txt files have exactly 300 characters.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
FILE *cfPtr = fopen("clients.txt", "r");
if(cfPtr == NULL)
{
puts("The file can't be open");
return 0;
}
char name[11], sex[11], dad[11], mom[11], line[300];
int age;
fgets(line, sizeof(line), cfPtr); //skip the first line
while(fgets(line, sizeof(line), cfPtr))
{
if(5 == sscanf(line, "%10s%10s%10d%10s%10s", name, sex, &age, dad, mom))
printf("%s, %s, %d, %s, %s\n", name, sex, age, dad, mom);
}
fclose(cfPtr);
return 0;
}
There is no such rule or restriction for .txt files.
As for taking 300 as the size of the line array and using it in fgets(line, sizeof(line), cfPtr);, it is only stating that fgets will take atmost 300 character inputs from a line (it can take less if there are less). Most probably, it is taken as an assumption that no line will have more than 300 characters so this is just an upper limit.
fgets(line, sizeof(line), cfPtr)
fgets reads in at most one less than sizeof(line) (300 bytes) from stream cfPtr and stores them into line.
The example below maybe help you:
#include <stdio.h>
int main()
{
FILE *fp = fopen("input.txt", "r");
char line[4]; // string length ups to 3 with this declaration.
while(fgets(line, sizeof(line), fp)) {
printf("%s\n",line);
}
printf("enter the string: \n");
fgets(line, sizeof(line), stdin);
printf("string from keyboard %s\n", line);
fclose (fp);
return 0;
}
The input file and output:
$cat input.txt
123456789
./test
123 // print 3 characters not 10 (one for enter character) characters of the line
456
789
enter the string:
1234567
line from keyboard 123
You can see the result. Even you type 1234567 from the keyboard, the string you got is 123 instead of 1234567.
This is one reason makes fgets is better and safer than gets when you enter string from stdin.
Related
First of all I am new to files in c, so it may be a simple question,
however I still didn't find a solution:
let's say that's the content of my file:
99
blah blah
...
...
I want to scan only the number from the beginning (it is always in a separate line)
My question is how to make it take the number (99) as one number and stop scanning.
int main(){
FILE* fp = fopen(file_name, "r");
int integer;
...
fclose(fp);
printf("%d", integer);
}
output for the file example:
99
-the nuber can be between 1 and 100-
I want to scan only the number from the beginning (it is always in a separate line).
That's a good hint, suggesting a line by line parsing of the input. You can use a combination of fgets(1) and sscan(2) to read that number.
fgets will read up to a certain number of character from a stream and store those character into a buffer. If it finds a newline, it stops reading, store the newline into the buffer followed by the null-terminator. Otherwise it only adds the terminator. If it fails, it returs a NULL pointer.
sscanf works basically like scanf or fscanf, but it reads from a character array, not from a stream.
It's also better to always check the return value of those library function.
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUF_SIZE 1024
int main(void)
{
char const *file_name = "data.txt";
FILE *in_file = fopen(file_name, "r");
if (!in_file) {
fprintf(stderr, "Error while reading \"%s\": %s", file_name, strerror(errno));
return EXIT_FAILURE;
}
char buffer[BUF_SIZE];
int number = 0;
while( fgets(buffer, BUF_SIZE, in_file) ) {
if ( sscanf(buffer, "%d", &number) == 1 ) {
if ( 0 < number && number < 100 ) {
printf("%d", number);
break;
}
}
}
fclose(in_file);
return EXIT_SUCCESS;
}
Example.
Some references of the functions used in the previous snippet
1) fgets: man-pages or cppreference.
2) sscanf: man-pages or cppreference
Why not use scanf? (fscanf to be more precise):
On success, the function returns the number of items of the argument
list successfully filled.
(source: cppreference)
So just check how many values did you read, if 0 that means it's not a number so you can just skip that string, for that you can use "%*" prefix to ignore the data.
You also said:
I want to scan only the number from the beginning (it is always in a
separate line)
so after you read the number just skip the whole line with "%*[^\n]" (reads
data until a new line symbol is encountered)
int num;
int scanReturn;
FILE* f = fopen("file.txt", "r");
...
do {
scanReturn = fscanf(f, "%d", &num);
if(scanReturn == 0)
{
scanReturn = fscanf(f, "%*s");
}
else if(scanReturn != EOF)
{
fscanf(f, "%*[^\n]");
printf("%d, ", num);
}
} while(scanReturn != EOF);
fclose(f);
This is the C code I have so far. I am reading the first name and last name from the input file but the thing that is giving me trouble is to print out the other stuff.
I have to take a line like this:
Venus Jensen 33770530841 vbjensen#oqtu.edu FRNO 624-771-4676 SIJ SBE WHV TVW
and remove the extra stuff to make it like this:
vbjensen Venus Jensen (624)771-4676
My problem is that I am getting the right output but for some of the lines that(1) don't have the FRNO or something equivalent and (2) not having the # symbol, the line still shows up. For example, the lines:
Noe Richard 974927158 nirichar#bvu.edu 079-651-3667 HAVQ
Phillip Sandoval 836145561 pusandov#luu.edu OXRU 697-728-1807 LHPN GUX
These lines should not be printed since the first one does not have the FRNO equivalent and the second one does not have the # symbol. Every time I try to add the format operation to match but not save, the program sscanf function starts to mess up.
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int main()
{
// Open the input file and print an error message if we're unsuccessful.
// (the error message is mostly to help you with debugging. We won't test
// this behavior).
FILE *fp = fopen("input.txt", "r");
char line[500];
if(!fp) {
printf("Can't open input file\n");
exit(1);
}
// Counting input lines, so we can report errors.
// Keep reading input lines until we reach the end-of-file.
// Write an output line or an error message for each one.
do {
int lineCount = 1;
char fName[12];
char lName[12];
//char skipNum[12];
char email[9];
//char firstNum[4];
//char secondNum[4];
//char thirdNum[5];
//printf("%c", ch);
char phone[] = "(123)123-1234";
//fscanf(fp, "%s", fName);
//fscanf(fp, "%s", lName);
//fscanf(fp, "%[1-9]", skipNum);
//fscanf(fp, "%[a-z]", email);
sscanf (line, "%11s%11s%*[ 0-9]%9[^#]%*[^0-9]%3c-%3c-%4c", lName, fName, email, &phone[1], &phone[5], &phone[9]);
//printf("Invalid line");
//printf("\n");
// exit(1);
printf("%s", line);
printf("\n");
printf("%s", email);
printf("%s", fName);
printf("%s", lName);
//printf("%s", skipNum);
//printf("%s", firstNum);
printf("%s", phone);
printf("\n");
lineCount++;
}
while (fgets(line, sizeof line, fp));
return EXIT_SUCCESS;
}
In the format string "%20s%20s%*[ 0-9]%20[^#]#%*s%20s %3c-%3c-%4c"
%20s will scan up to 20 non-whitespace characters. Ignoring leading whitespace and stopping at trailing whitespace.
%*[ 0-9] will scan spaces and digits. The asterisk, *, tells sscanf to discard the scanned characters.
%20[^#]# will scan up to 20 characters or will stop scanning at a #. Then it will try to scan a #. If the # is missing the scan will terminate early.
%*s will scan non-whitespace and discard the characters.
%20s will scan up to 20 non-whitespace characters.
%3c will ignore any leading whitespace and scan three characters.
-%3c will scan a - and then three characters. If the - is missing the scan will terminate early.
-%4c will scan a - and then four characters. If the - is missing the scan will terminate early.
If sscanf does not scan seven items, nothing will be printed.
#include <stdio.h>
#include <stdlib.h>
int main ( void) {
char line[500] = "";
int lineCount = 0;
FILE *fp = NULL;
if ( NULL == ( fp = fopen("input.txt", "r"))) {
fprintf( stderr, "Can't open input file\n");
exit(1);
}
while ( fgets ( line, sizeof line, fp)) {//read each line from the file
char fName[21];
char lName[21];
char match[21];
char email[21];
char phone[] = "(123)567-9012";
lineCount++;
if ( 7 == sscanf ( line, "%20s%20s%*[ 0-9]%20[^#]#%*s%20s %3c-%3c-%4c"
, lName, fName, email, match, &phone[1], &phone[5], &phone[9])) {
printf ( "line [%d] %s %s %s %s\n", lineCount, email, fName, lName, phone);
}
}
fclose ( fp);
return 0;
}
As the title indicates I'm trying to make a program that verifies if a word is in a file and prints the number of the line and the line itself. The exercise tells us to use the function strstr.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DIM_WORDS 1000
int main(void)
{
char word[DIM_WORDS]={'\0'};
char textname[DIM_WORDS]={'\0'};
char stringtoread[DIM_WORDS]={'\0'};
char line[DIM_WORDS]={'\0'};
char* verify;
FILE* fp=NULL;
int ret=0 , num=0;
do{
printf("Introduza o nome do ficheiro e a palavra a procurar\n");
// Lê ambas as informações e guarda-as numa string
fgets(stringtoread, DIM_WORDS, stdin);
// Separa as duas informações em 2 strings
ret=sscanf(stringtoread, "%s %s", textname, word);
}while(ret!=2);
fp=fopen(textname, "r");
if(fp==NULL)
{
printf("Error: That file doesn't exist or could not be open\n");
exit(EXIT_FAILURE);
}
while(fgets(line, sizeof(line), fp))
{
verify = strstr(word, line);
if(verify!=NULL)
{
printf("The word was encountered in line %d\n", num);
printf("%s", line);
}
num++;
}
fclose(fp);
return EXIT_SUCCESS;
}#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DIM_WORDS 1000
int main(void)
{
char word[DIM_WORDS]={'\0'};
char textname[DIM_WORDS]={'\0'};
char stringtoread[DIM_WORDS]={'\0'};
char line[DIM_WORDS]={'\0'};
char* verify;
FILE* fp=NULL;
int ret=0 , num=0;
do{
printf("Introduza o nome do ficheiro e a palavra a procurar\n");
fgets(stringtoread, DIM_WORDS, stdin);
ret=sscanf(stringtoread, "%s %s", textname, word);
}while(ret!=2);
fp=fopen(textname, "r");
if(fp==NULL)
{
printf("Error: That file doesn't exist or could not be open\n");
exit(EXIT_FAILURE);
}
while(fgets(line, sizeof(line), fp))
{
verify = strstr(word, line);
if(verify!=NULL)
{
printf("The word was encountered in line %d\n", num);
printf("%s", line);
}
num++;
}
fclose(fp);
return EXIT_SUCCESS;
}
I tried my program with a file text.txt and trying to find the word file:
A text file (sometimes spelled "textfile": an old alternative name is "flatfile") is a kind of computer file that is structured as a sequence of lines of electronic text.
A text file exists within a computer file system.
The end of a text file is often denoted by placing one or more special characters, known as an end-of-file marker, after the last line in a text file.
Such markers were required under the CP/M and MS-DOS operating systems.
On modern operating systems such as Windows and Unix-like systems, text files do not contain any special EOF character.
"Text file" refers to a type of container, while plain text refers to a type of content. Text files can contain plain text, but they are not limited to such.
At a generic level of description, there are two kinds of computer files: text files and binary files.[1]
Problem is my program is not printing anything.
I think the problem might be here:
while(fgets(line, sizeof(line), fp))
{
verify = strstr(word, line);
if(verify!=NULL)
{
printf("The word was encountered in line %d\n", num);
printf("%s", line);
}
num++;
}
Since if I remove most of the code just to print all the lines of the file it prints them:
while(fgets(line, sizeof(line), fp)
printf("%s", line);
Can someone help me to fix this? Thanks!
You've got this backwards
verify = strstr(word, line);
you're searching for line in word when it should be
verify = strstr(line, word);
I am currently learning reading from files in C.
Anyway, cutting to the chase:
Text file content:
123456 James Doakes; 0
987987 Dexter Morgan; 0
010203 Masuka Perv; 0
int main()
{
char accountNr[ACCOUNTNRSIZE], ownerName[NAMESIZE], enter[3];
int accountBalance = 0;
char filename[] = "breg.txt";
FILE *file = fopen(filename, "r");
if (file != NULL) {
char line[128];
while (fgets(line, sizeof(line), file) != NULL) {
sscanf(line, "%s %[^;] %d ", accountNr, ownerName, &accountBalance);
printf("%s", ownerName);
//fflushstdin();
}
fclose(file);
} else {
perror(filename);
}
return 0;
}
I wrote this to check if the name for instance James Doakes was registered correctly :
printf("%s", ownerName);
But when it prints that out it's like the stdout is still active and I can push Enter and it will type the name again. My goal is to of course be able to sscanff the number, the full name, and the last number as seperate variables. But it obviously doesn't work. I am guessing a \n gets registered as well. Dunno, I am just speculating.
What am I doing wrong? Why? And how do I solve this?
Much appreciated,
Mif
%s %[^;] %d
means a string terminated by white space, optional white space, a sequence of characters that are not ;, optional white space, then a number.
You appear to be not scanning for the actual ; character itself so that, when you try to get the number, the ; in the input stream will cause it to fail. You can see this with:
#include <stdio.h>
#define ACCOUNTNRSIZE 100
#define NAMESIZE 100
int main (void) {
char accountNr[ACCOUNTNRSIZE], ownerName[NAMESIZE], enter[3];
int accountBalance = 0;
char filename[] = "breg.txt";
FILE *file = fopen(filename, "r");
if (file != NULL) {
char line[128];
while (fgets(line, sizeof(line), file) != NULL) {
int count = sscanf(line, "%s %[^;] %d ", accountNr, ownerName, &accountBalance);
printf ("%d [%s] [%s] [%d]\n", count, accountNr, ownerName, accountBalance);
}
fclose(file);
} else {
perror(filename);
}
return 0;
}
which outputs:
2 [123456] [James Doakes] [0]
2 [987987] [Dexter Morgan] [0]
2 [010203] [Masuka Perv] [0]
In fact, even if you change the breg.txt file to be:
123456 James Doakes; 314159
987987 Dexter Morgan; 271828
010203 Masuka Perv; 42
you still get 0 for the account balance because the scanning only successfully reads two items.
Whenever you use one of the scanf-family functions, you should check the return code to ensure it's scanning the correct number of items, as in:
int count = sscanf (line, "%s %[^;] %d ", accountNr, ownerName, &accountBalance);
if (count != 3) {
fprintf (stderr, "Catostrophic failure, count is %d\n", count);
return 1;
}
The fix here is relatively simple, just use %s %[^;]; %d as the format string.
With that change, the output you see is:
3 [123456] [James Doakes] [314159]
3 [987987] [Dexter Morgan] [271828]
3 [010203] [Masuka Perv] [42]
Keep in mind you don't actually need a space before the %d (though it causes no harm). That particular format specifier skips white space before attempting to scan the number.
Trying to create a program that takes in a text file and reads it line by line. It then finds the two integers that are on each line and adds them together. It then outputs the new line with the original string and total to a new text file. I need help adding the two integers, getting them from each line, and then putting the new line to a text file.
input text file
good morning hello 34 127
ann 20 45
10 11
fun program and you find the same 90 120
news paper said that 56 11
how do you like 20 5
line number 90 34
Outputs first like would look like: and then continue on
good morning hello 161
Code:
int processTextFile(char * inputFileName, char * outputFileName)
{
FILE *fp = fopen(inputFileName, "r");//open file to to read
char buff[1024];
char *p, *p1;
int num;
while (fgets(buff, 1024, fp)!=NULL)
{
printf("%s\n", buff);
while(scanf(buff, "%*[^0-9]%d", &num)== 1)
printf("%d\n", num);
//fscanf(fp, "%s", buff);
}
return 0;
}
EDIT!!!!::
So now that I've been able to accomplish this. How would I sort it by the number produced? for example:
Time is money 52
here I am 3
21
Would output to a new text file in order like
here I am 3
21
Time is money 52
My version using strcspn() is supposed to work with stdin for input and stdout for output. (so you can do executable <textfile >newtextfile)
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char line[1000];
while (fgets(line, sizeof line, stdin)) {
char *ptr;
size_t x = strcspn(line, "0123456789");
if (line[x]) {
errno = 0;
int n1 = strtol(line + x, &ptr, 10);
if (*ptr && !errno) {
errno = 0;
int n2 = strtol(ptr, &ptr, 10);
if (*ptr && !errno) {
int n3 = n1 + n2;
printf("%.*s%d\n", (int)x, line, n3);
} else {
printf("%s", line); // line includes ENTER
}
} else {
printf("%s", line); // line includes ENTER
}
} else {
printf("%s", line); // line includes ENTER
}
}
return 0;
}
The same version without the error checking
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char line[1000];
while (fgets(line, sizeof line, stdin)) {
char *ptr;
size_t x = strcspn(line, "0123456789");
int n1 = strtol(line + x, &ptr, 10);
int n2 = strtol(ptr, &ptr, 10);
int n3 = n1 + n2;
printf("%.*s%d\n", (int)x, line, n3);
}
return 0;
}
The approach should be:
open two files, one for input, one for output.
use sscanf() to read the input buffer.
scan the leading string, and then two number.
if previous sscanf() fails, only check for two number.
if either of the above scanning is success, print the sum to the output file.
A sample code, should look like
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
FILE *fpin = fopen("ipfile", "r");//open file to to read
if (!fpin)
{
printf("Error in ipfile opening\n");
exit (-1);
}
FILE *fpout = fopen("opfile", "w");//open file to to write
if (!fpout)
{
printf("Error in opfile opening\n");
exit (-1);
}
char buff[1024] = {0};
char str[1024] = {0};
int num1 =0, num2= 0;
while (fgets(buff, 1024, fpin)!=NULL)
{
memset(str, 0, sizeof(str));
//printf("%s\n", buff);
if(sscanf(buff, "%[^0-9]%d %d", str, &num1, &num2)== 3)
fprintf(fpout, "%s %d\n", str, (num1+num2));
else if (sscanf(buff, "%d %d", &num1, &num2)== 2)
fprintf(fpout, "%d\n", (num1+num2));
}
return 0;
}
Note:
The above procedure, is a kind of workaround. If the data pattern in the file changes, lot of changes will be required to maintain a code like that. Instead of usinf sscnaf(), for a better and roubust approach, you should
read the line from file
start tokenizing the input buffer (strtok()) and check for ints as tokens (strtol()).
save the returned tokens and ints seperately.
once the strtok() returns NULL, you print the string tokens and the sum of the ints to the o/p file.
I intend not to change your code completely, so I just added some snippets for improvement.
int processTextFile(char *inputFileName, char *outputFileName) {
FILE *fp = fopen(inputFileName, "r");
FILE *out = fopen(outputFileName, "w");
char line[1024];
if (!fp) {
perror(inputFileName);
return;
}
while (fgets(line, sizeof(line), fp) != NULL) {
int num1 = 0, num2 = 0;
char textPart[1024] = "";
if ( !sscanf(line, "%[a-zA-Z' ']%d%d", textPart, &num1, &num2) {
sscanf(line, "%d%d", &num1, &num2);
}
fprintf(out, "%s %d\n", textPart, num1 + num2);
}
fclose(fp);
fclose(out);
}
Explanation:
I scanned the text file, extracted the text part and the two ints. Since I noticed that the ints are placed at the end of each line, I just used sscanf() for that matter.
sscanf(line, "%[a-zA-Z' ']%d%d", textPart, &num1, &num2);
Here, "%[a-zA-Z' ']%d%d" format specifiers means to get only alphabets and spaces.
Since it will only get letters and spaces, the line "10 11" in your input file won't be put to num1 and num2. Because the code inspects first for a string containing letters and spaces. Since 10 and 11 are not of the qualified types, then the line is just skipped.
That's why I added an if-else statement, which checks if sscanf wrote anything to memory. If sscanf returned 0, then it means that no text part is present. Just digits. So the program will scan the two digits.
if ( !sscanf(line, "%[a-zA-Z' ']%d%d", textPart, &num1, &num2) ) {
sscanf(line, "%d%d", &num1, &num2);
}
I also added file checking for input file. It checks if file doesn't exist or can't be opened by the filestream.
if (!fp) {
perror(inputFileName);
return;
}
Here is the content of output file after execution:
good morning hello 161
ann 65
21
fun program and you find the same 210
news paper said that 67
how do you like 25
line number 124