How to read specific string from each line? - c

I'm a little desperate because I don't know how to create a program that only reads some data (words or numbers) from an input file and then with this data writes another file but putting them in a tabulated order ...
I don't know how to make the program look in the line of the input file for example "number of sequences: 2" and to make that it takes only the data "2" to be stored in the new file ...
Please help me
I'm just starting
Thank you all

The issue you are having is not with the loop, and not with the eof.
The real issue is you have incorrect parsing logic.
Your input file is not uniformed:
Different session lines have different "MODE" in them
Number of blank lines varies from group to group
Blank lines may actually contain any number of space characters
"Number of sequences" line appears in different places in different groups
To parse such a file you need a more flexible logic that will check each input line, collect all the data needed to build an output line, and only then print it to the output file.
To do this, you can use one loop reading only one line at a time, and then testing its contents using the strncmp function.
Once you identified the type of data the line contains, save it to a variable using sscanf function.
Here is the code that will do the job:
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv) {
FILE *file_in, *file_out;
char line[200];
/* intialize these just in case we want to validate the input file */
int current_session = 0;
int current_sequences = 0;
int current_registration = 0;
/* these arrays can probably be smaller */
char chars_given[200] = { 0 };
char chars_recognized[200] = { 0 };
file_in = fopen("summary.txt", "r");
if (file_in == NULL) {
perror("Error opening input file");
return 1;
}
file_out = fopen("ordinated.txt", "w");
if (file_out == NULL) {
perror("Error opening output file");
return -1;
}
while (fgets(line, 200, file_in) != NULL) {
/* check if this is start of session using safe string comparison */
if (strncmp(line, "session", strlen("session")) == 0) {
sscanf(line, "session %d", &current_session);
} else if (strncmp(line, "number of sequences", strlen("number of sequences")) == 0) {
sscanf(line, "number of sequences: %d", &current_sequences);
} else if (strncmp(line, "registration", strlen("registration")) == 0) {
sscanf(line, "registration %d", &current_registration);
} else if (strncmp(line, "characters given", strlen("characters given")) == 0) {
sscanf(line, "characters given: %s", chars_given);
} else if (strncmp(line, "characters recognized", strlen("characters recognized")) == 0) {
sscanf(line, "characters recognized: %s", chars_recognized);
} else {
/* This is a line with no information (blank or separator).
Time to print results we collected, and reset the variables
for the next set of results. */
/* check we have enough information to output a line */
if (current_session > 0 && current_sequences > 0 &&
current_registration > 0 && strlen(chars_given) > 0) {
/* check if anything was recognized */
if (strlen(chars_recognized) > 0) {
fprintf(file_out, "%d %d %d %s %s\n", current_session, current_registration,
current_sequences, chars_given, chars_recognized);
} else { /* one less parameter to output if nothing was recognized */
fprintf(file_out, "%d %d %d %s\n", current_session, current_registration,
current_sequences, chars_given);
}
/* Now reset for next time. If you don't do this, the output line will repeat */
current_registration = 0;
chars_given[0] = '\0';
chars_recognized[0] = '\0';
}
}
}
/* the last block may not be printed in the loop if there is no empty line after it */
if (current_session > 0 && current_sequences > 0 &&
current_registration > 0 && strlen(chars_given) > 0) {
/* check if anything was recognized */
if (strlen(chars_recognized) > 0) {
fprintf(file_out, "%d %d %d %s %s\n", current_session, current_registration,
current_sequences, chars_given, chars_recognized);
} else { /* one less parameter to output if nothing was recognized */
fprintf(file_out, "%d %d %d %s\n", current_session, current_registration,
current_sequences, chars_given);
}
}
fclose(file_in);
fclose(file_out);
return 0;
}
This code is a bit ugly, but I tried to keep it simple.
It can be cleaned up by using structures, some flags, and moving some of the code to separate functions.
Edit: this code omits sanity checks for simplicity, and assumes the input file is not corrupt, i.e. first non empty line is always session, lines contain all the information they should, etc.

Related

How do I make the file pointer point at the very start of the file?

I'm having trouble setting the file pointer to the very start of a file to write some stuff at first AFTER having already written some text in it.
I've tried rewind(), fseek(), opening the file in "r+" & "a+" modes, nothing seems to work.
Here's a small recreation of the program:
#include<stdio.h>
#include<stdlib.h>
void master_globalprint(int lim)
{
int i = 0;
FILE* maspass;
errno_t err;
err = fopen_s(&maspass, "Master_Password.txt", "r+");
if (err != 0)
{
printf("Error opening Master_Password.txt");
exit(0);
}
rewind(maspass);
printf("Pointing to %ld", ftell(maspass));
while (i < lim)
{
fprintf(maspass, "%d", i); //Writing the array infront of the encrypted code
i++;
}
fclose(maspass);
}
void master_create() //To Create a Master Password
{
int count = 0;
char pass;
FILE* maspass;
errno_t err;
err = fopen_s(&maspass, "Master_Password.txt", "a");
if (err != 0)
{
printf("Error creating Master_Password.txt");
exit(0);
}
printf(" Enter Master Password : ");
while ((pass = getchar()) != EOF && pass != '\n')
{
count++;
fprintf(maspass, "%c", pass); //The characters are then printed one by one
}
if (count == 0)
{
remove("Master_Password.txt");
printf("Master Password cannot be empty");
exit(0);
}
fprintf(maspass, "%c", (count + 33)); //To put the amount of letters into file, forwarded by 33 to reach a certain ASCII threshold and converted to char
fprintf(maspass, "\n");
fclose(maspass);
master_globalprint(count);
}
void main()
{
master_create();
}
The above functions work and print the correct values except the master_globalprint function starts printing exactly where the last function left off.
Is it because I've to use command line arguments to achieve the task? If so, can I set the command line arguments to be executed by default somehow so that if the code is distributed, the user won't have to bother?
EDIT : Added in a reproducible code sample. When I put "a" in line 31, it prints only the stuff I input and not the numbers in master_globalprint(). If I put "w", it ONLY prints the numbers in master_globalprint() and not the stuff I input.
Here the writing mode should be w+:
err = fopen_s(&maspass, "Master_Password.txt", "a" /* w+ */);
Here you should close the file and then remove it:
if (count == 0)
{
/*fclose_s(maspass);*/
remove("Master_Password.txt");
printf("Master Password cannot be empty");
exit(0);
}
Instead of doing this, you should keep the file descriptor open and pass it to master_globalprint:
fclose(maspass);
master_globalprint(count);
/* master_globalprint(count, maspass);
* fclose(maspass); */
Then keep reusing the open file descriptor.

How to detect last extra Empty Line and Ignore it in C (using getc)

Hello I am using this code for reading floating numbers in txt. If end of txt file has extra blank empty line program reads it 0.00000 and this affect my calculation Last empty line
(# means end of the calculation I added comment line if it exist update comment line)
I try "getline" and other function I can't fix it
fptr = fopen(fileName,"r+");
if(fptr == NULL){
printf("Error!! Cannot open file: %s \n", fileName );
return 1;
}
else {
printf("File opened successfully\n");
while((c = getc(fptr)) != '#' && c != EOF) {
fscanf(fptr,"%f" ,&arr[i]);
++i;
}
}
Check the return value of fscanf -- it should return 1 when it successfully reads a number and 0 on that blank line.
OP is reading a file, line by line and has 4 outcomes:
Successful translated to a number.
Line begins with a # or whitespace only.
No more input (end-of-file).
Something else.
Suggest new approach: read line by line as text and then attempt various parsings. It is important to check the return value of *scanf() to help determine success.
printf("File opened successfully\n");
float /* or double */ arr[N];
size_t i = 0;
char buffer[100];
while (fgets(buffer, sizeof buffer, fptr)) {
double x;
char sof[2];
if (sscanf(buffer, "%lf" ,&x) == 1) {
if (i < N) {
arr[i++] = x;
} else {
puts("No room");
}
} else if (sscanf(buffer, "%1s" , sof) != 1 || sof[0] == '#') {
; // quietly ignore white-space only lines and that begin with #
} else {
puts("Unexpected input");
}
}
fclose(fptr);
puts("Done");

how to read specific lines of a text document and write them to another text | C

I have created a function that takes as a parameter the name of a source file, the name of a destination file and the beginning and end lines of the source file lines that will be copied to the destination file, like the example below. All I want to do is to input the lines that I want to copy to the other text file like the example below:
The code I show you just "reads" the content of the one text file and "writes" another one. I want to "write" specific lines that the user gives, not the whole text file
Inputs by the user:
Source_file.txt //the file that the destination file will read from
destination_file.txt //the new file that the program has written
2 3 // the lines that it will print to the destination file: 2-3
Source_file.txt:
1
2
3
4
5
6
destination_file.txt
2
3
code:
#include <stdio.h>
#include <stdlib.h>
void cp(char source_file[], char destination_file[], int lines_copy) {
char ch;
FILE *source, *destination;
source = fopen(source_file, "r");
if (source == NULL) {
printf("File name not found, make sure the source file exists and is ending at .txt\n");
exit(EXIT_FAILURE);
}
destination = fopen(destination_file, "w");
if (destination == NULL) {
fclose(source);
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
while ((ch = fgetc(source)) != EOF)
fputc(ch, destination);
printf("Copied lines %d from %s to %s \n",
lines_copy, source_file, destination_file, ".txt");
fclose(source);
fclose(destination);
}
int main() {
char s[20];
char d[20];
int lines;
printf("-Enter the name of the source file ending in .txt\n"
"-Enter the name of the destination file ending in .txt\n"
"-Enter the number of lines you want to copy\n\n");
printf(">subcopy.o ");
gets(s);
printf("destination file-> ");
gets(d);
printf("Lines: ");
scanf("%d", &lines);
cp(s, d, lines);
return 0;
}
In cp(), in order to select the lines to keep, you have to know their position in the input-file. Thus, you need to count lines.
Using fgets instead of fgetc will allow you to count the lines.
On the other hand, if I wanted to select lines 3 and 7 to 12 in a file, I'd use:
sed -n -e "3p;7,12p" < input.txt > output.txt
this is a very simple solution, let's say you know that the maximun length of a line will be 100 characters for simplicity (if a line is longer than 100 characters only the first 100 will be taken)
at the top (outside main) you can write
#ifndef MAX_LINE_SIZE
#define MAX_LINE_SIZE 100
#endif
i know many people don't like this but i think in this case it makes the code more elegant and easier to change if you need to modify the maximum line size.
to print only the wanted lines you can do something like this
char line[MAX_LINE_SIZE];
int count = 0;
while (fgets(line, MAX_LINE_SIZE, source)){
count++;
if (3 <= count && count <= 5){
fputs(line, destination);
}
}
The while loop will end when EOF is reched because fgets returns NULL.
P.S. there could be some slight errors here and there since i wrote it pretty fast and going by memory but in general it should work.
There are some problems in your program:
Do not use gets(), it may cause buffer overflows.
Always use type int to store the return value of fgetc() in order to distinguish EOF from regular byte values.
You pass an extra argument ".txt" to printf(). It will be ignored but should be removed nonetheless.
To copy a range of lines from source to destination, you can just modify your function this way:
#include <stdio.h>
#include <string.h>
#include <errno.h>
void cp(char source_file[], char destination_file[], int start_line, int end_line) {
int ch;
int line = 1, lines_copied;
FILE *source, *destination;
source = fopen(source_file, "r");
if (source == NULL) {
printf("Cannot open input file %s: %s\n",
source_file, strerror(errno));
exit(EXIT_FAILURE);
}
destination = fopen(destination_file, "w");
if (destination == NULL) {
printf("Cannot open output file %s: %s\n",
destination_file, strerror(errno));
fclose(source);
exit(EXIT_FAILURE);
}
while ((ch = fgetc(source)) != EOF) {
if (line >= start_line && line <= end_line) {
fputc(ch, destination);
}
if (ch == '\n') {
line++;
}
}
lines_copied = 0;
if (line > start_line) {
if (line >= end_line) {
lines_copied = end_line - start_line + 1;
} else {
lines_copied = line - start_line + 1;
}
}
printf("Copied lines %d from %s to %s\n",
lines_copy, source_file, destination_file);
fclose(source);
fclose(destination);
}
int main() {
char source_file[80];
char destination_file[80];
int start_line, end_line;
printf("-Enter the name of the source file ending in .txt\n"
"-Enter the name of the destination file ending in .txt\n"
"-Enter the start and end line\n\n");
printf(">subcopy.o ");
if (scanf("%79s", source_file) != 1) {
return 1;
}
printf("destination file-> ");
if (scanf("%79s", destination_file) != 1) {
return 1;
}
printf("Start and end lines: ");
if (scanf("%d %d", &start_line, &end_line) != 2) {
return 1;
}
cp(source_file, destination_file, start_line, end_line);
return 0;
}

Read in text file with multiple elements per line

I need to read in a text file, "input.in", so that I can run a sort function on the code, according to id. The input.in file contains an id and name of file, 8 lines total. I know that I need to read in the input file line by line (not sure if my code is correct). But the main problem is that the fopen function is returning the result that it can't find the input file, even though its on the desktop along with the source file being saved there.
Any tips would be greatly appreciated
int main()
{
int id;
char node;
char item[9], status;
FILE *fp;
if((fp = fopen("/Users/jacobsprague/Desktop/input.txt", "r+")) == NULL)
{
printf("No such file\n");
exit(1);
}
while(42)
{
int ret = fscanf(fp, "%s %c", id, &node);
if(ret == 2)
printf("\n%s \t %c", id, node);
else if(errno != 0)
{
perror("scanf:");
break;
}
else if(ret == EOF)
{
break;
}
else
{
printf("No match.\n");
}
}
printf("\n");
if(feof(fp))
{
puts("EOF");
}
return 0;
}
Here is the input file contents:
8
4 Node1111
8 Node11111111
2 Node11
7 Node1111111
1 Node1
5 Node11111
6 Node111111
3 Node111
fopen can fail for reasons other than not finding the file, so you should check errno to see what the problem was. However in this case, as BLUEPIXY has mentioned, the problem appears to be that you have typed input.txt instead of input.in.
// 1) there were lots of little oops in the ops code,
// 2) the op skipped the detail that the first line contains
// a count of the number of following lines
// all of that is corrected in the following
#include <stdio.h>
#include <stdlib.h>
int main()
{
int id; // value read from file
char node[30]; // string read from file
//char item[9]; // if not commented, raises compiler warning about unused variable
//char status; // if not commented, raises compiler warning about unused variable
int ret; // returned value from fscanf
int lineCount = 0; // number of lines in file after first line
int i; // loop counter
FILE *fp;
if((fp = fopen("/Users/jacobsprague/Desktop/input.in", "r")) == NULL)
{
// perror also outputs the value of errno and the results of strerror()
perror( "fopen failed for file: input.in");
exit(1);
}
// implied else, fopen successful
// get first line, which contains count of following lines
if( 1 != (ret = fscanf(fp, " %d", &lineCount)) )
{ // fscanf failed
perror( "fscanf"); // this also outputs the value of errno and the results of strerror()
exit( EXIT_FAILURE );
}
// implied else, fscanf successful for lineCount
for( i=0; i < lineCount; i++) // read the data lines
{
// note leading space in format string to consume white space (like newline)
if( 2 != (ret = fscanf(fp, " %d %s", &id, node)) )
{ // fscanf failed
// this also outputs the value of errno and the results of strerror()
perror( "fscanf for id and node failed");
break;
}
// implied else, fscanf successful for id and node
printf("\n%d\t %s", id, node);
} // end for
printf("\n");
if( EOF == ret )
{
puts("EOF");
} // endif
return 0;
} // end function: main

In C, how do I read a file, and store only the doubles - ignoring text before?

I need to read in a file that contains text, and then a double for that text. It is simply to get the mean and standard deviation for the set of numbers, so the text that comes before is irrelevant. For example, my input file looks a little like:
preface 7.0000
chapter_1 9.0000
chapter_2 12.0000
chapter_3 10.0000
etc..
In this case, it is finding the mean and std dev for the chapters of a book. I have the section of code below, but I'm not quite sure how to "ignore" the text, and only grab the doubles. At the moment this code prints out zeros and only exits the loop when it exceeds the array limit, which I set as a constant to 20 at the beginning of the program.
FILE *ifp;
char *mode = "r";
ifp = fopen("table.txt", mode);
double values[array_limit];
int i;
double sample;
if (ifp==NULL)
{
printf("cannot read file \n");
}
else
{
i = 0;
do
{
fscanf(ifp, "%lf", &sample);
if (!feof(ifp))
{
values[i] = sample;
printf("%.4lf \n", values[i]);
i++;
if (i>=array_limit) //prevents program from trying read past array size limit//
{
printf("No more space\n");
break;
}
}
else
{
printf("read complete\n");
printf("lines = %d\n", i);
}
}while (!feof(ifp));
fclose(ifp);
}
I think you could use fscanf(ifp, "%*[^ ] %lf", &sample) for reading from your file. The * says to ignore that particular match, the [] specifices a list of characters to match and the ^ indicates to match all characters except those in [].
Or possibly (a bit simpler) fscanf(ifp, "%*s %lf", &sample).
You have two major problems -- you're using feof which is pretty much always wrong, and you're not checking the return value of fscanf, which it what tells you whether you got a value or not (or whether you got to the eof).
So what you want is something like
while ((found = fscanf(ifp, "%lf", &values[i])) != EOF) { /* loop until eof */
if (found) {
/* got a value, so count it */
if (++i >= ARRAY_LIMIT) {
printf("no more space\n");
break;
}
} else {
/* something other than a value on input, so skip over it */
fscanf(ifp, "%*c%*[^-+.0-9]");
}
}
When reading in from a file, it's often best to use fgets to read one line at a time, then extract the parts you are interested in using sscanf:
#include <stdlib.h>
#include <stdio.h>
#define ARRAY_LIMIT 10
#define LINE_LENGTH 128
int main()
{
double values[ARRAY_LIMIT];
int i, count = 0;
double sample;
FILE *ifp = fopen("table.txt", "r");
if (ifp==NULL)
{
printf("cannot read file \n");
return 1;
}
char buff[LINE_LENGTH];
while (fgets(buff, LINE_LENGTH, ifp) != NULL)
{
if (sscanf(buff, "%*s %lf", &sample) != 1) break;
values[count++] = sample;
if (count == ARRAY_LIMIT) {
printf("No more space\n");
break;
}
}
fclose(ifp);
for (i = 0; i < count; ++i) {
printf("%d: %f\n", i, values[i]);
}
return 0;
}
fgets returns NULL if it encounters the end of the file, or if a read error has occurred. Otherwise, it reads one line of the file into the character buffer buff.
The asterisk %*s in the sscanf means that the first part of the line is discarded. The second part is written to the variable sample. I am checking the return value of sscanf, which indicates how many values have been read successfully.
The loop breaks when the end of the file is reached or the count reaches the size of the array.

Resources