How to skip a comment in c programming with using fopen - c

I want to ignore/skip the comments in a text file when I use fgets.
The problem is that I only can skip a comment if the first character in a line starts is #. Comments starts with # in my text file. But there are some # in my file.txt that are not the first character of a line, like so;
#Paths
A B #Path between A and B.
D C #Path between C and D.
A is my first node, B is my second node and when # comes I want to ignore the rest of text until the next line. My new node should be D and C etc. I can only use "r" in fopen function.
I have tried fgets but it reads line by line and fgetc doesn't help either.
bool ignore_comments(const char *s)
{
int i = 0;
while (s[i] && isspace(s[i])) i++;
return (i >= 0 && s[i] == '#');
}
FILE *file;
char ch[BUFSIZE];
file = fopen("e.txt", "r");
if (file == NULL) {
printf("Error\n");
fprintf(stderr, "ERROR: No file input\n");
exit(EXIT_FAILURE);
}
while(fgets(ch, BUFSIZE, file) != NULL)
{
if (line_is_comment(ch)) {
// Ignore comment lines.
continue;
printf("%c",*ch);
}
fscanf(file, "%40[0-9a-zA-Z]s", ch);
....
}

the following proposed code:
performs the desired functionality
cleanly compiles
properly checks for errors
this answer uses a state machine, based on: 'InComment'
and now, the proposed code:
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
int InComment = 0;
FILE *fp = fopen( "file.txt", "r" );
if( !fp )
{
perror( "fopen to read -file.txt- failed" );
exit( EXIT_FAILURE );
}
int ch;
while( (ch = fgetc(fp)) != EOF )
{
if( ch == '#' )
{
InComment = 1;
}
else if( ch == '\n' )
{
InComment = 0;
fputc( ch, stdout );
}
else if( !InComment )
{
fputc( ch, stdout );
}
}
fclose( fp );
}

Also method names are different, but am I right with this version ?
Ignore my dirty method line_is_comment - from first version unless you want to play with ;-)
Extended test input:
#Paths
A B #Path between A and B.
D C #Path between C and D.
E F
G H
Output:
rest of line read
AB rest of line read
DC rest of line read
EF rest of line read
GH rest of line read
#include <stdio.h>
bool line_is_comment(const char *s)
{
char *commentPos = const_cast<char*>(strchr(s, '#'));
if(commentPos != NULL) {
*commentPos = 0; // cut-off chars after comment
//return true; // or false then to accept the line
return commentPos == s;
}
return false;
}
#define BUFSIZE 50
int main()
{
FILE *file;
char ch[BUFSIZE];
file = fopen("e.txt", "r");
if (file == NULL) {
printf("Error\n");
fprintf(stderr, "ERROR: No file input\n");
exit(EXIT_FAILURE);
}
int x;
while(!feof(file)) {
x = fscanf(file, "%40[0-9a-zA-Z]s", ch);
if(x == 0) {
ch[0] = fgetc(file);
if(ch[0] == '#' || ch[0] == '\n') {
if(ch[0] != '\n') fgets(ch, BUFSIZE, file);
printf(" rest of line read\n");
}
} else if(x<0) break;
else {
printf("%c",*ch); // continue with ... undisclosed part here
}
}
return 0;
}

You can also make use of strcspn to trim all comments (and if not present, trim the line-endings from your buffer) in a single simple call. Where you would normally trim the line-ending from the buffer read by fgets() with:
ch[strcspn (ch, "\r\n")] = 0; /* trim line-ending */
You can simply add the "#" character to your reject list and nul-terminate there if a comment is present. That would reduce the complete task of removing comments beginning with '#' and outputting the newly formatted line to:
while (fgets (ch, BUFSIZE, fp)) { /* read every line */
ch[strcspn (ch, "#\r\n")] = 0; /* trim comment or line-ending */
puts (ch); /* output line w/o comment */
}
A short example taking the file to read as the first argument to the program (or reading from stdin by default if no argument is given), you could do:
#include <stdio.h>
#include <string.h>
#define BUFSIZE 1024 /* if you need a constant, #define one (or more) */
int main (int argc, char **argv) {
char ch[BUFSIZE];
/* use filename provided as 1st argument (stdin by default) */
FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;
if (!fp) { /* validate file open for reading */
perror ("file open failed");
return 1;
}
while (fgets (ch, BUFSIZE, fp)) { /* read every line */
ch[strcspn (ch, "#\r\n")] = 0; /* trim comment or line-ending */
puts (ch); /* output line w/o comment */
}
if (fp != stdin) fclose (fp); /* close file if not stdin */
return 0;
}
Example Input File
Borrowing Tom's example file :)
$ cat dat/comments_file.txt
#Paths
A B #Path between A and B.
D C #Path between C and D.
E F
G H
Example Use/Output
$ ./bin/comments_remove <dat/comments_file.txt
A B
D C
E F
G H
Look things over and let me know if you have further questions.

Related

In C, how can I produce an error if the input string is too big?

I want to read a list of words from a file, which has one word per line.
The words should have up to 4 characters each. How can I produce an error if one of the lines is longer than that?
I tried reading the words using fgets
char buf[5];
fgets(buf, 5, stdin);
and with scanf
char buf[5];
scanf("%4s", &buf);
but in both cases it splits long lines into smaller lines. For example qwerasdf is read as two words, qwer and asdf. Is there a way to detect that it tried to read a long line with more than 4 characters and give an error instead?
The only alternative I can think of is reading the input character-by-character and taking care of everything by myself. But is there a simpler solution using functions from the standard library?
You could check for the length of the read string and since fgets also reads the newline character, you could explicitly check for '\n' as the last input character.
char buf[6];
while (fgets(buf, sizeof(buf), stdin)) {
if (strlen(buf) > 5
|| (strlen(buf) == 5 && buf[strlen(buf) - 1] != '\n')) {
fprintf(stderr, "line too long\n");
exit(EXIT_FAILURE);
}
}
The buffer must consist of at least six characters: 4 input characters + 1 newline character + the string terminating NUL byte.
You are making an excellent choice reading with fgets(), the only rule-of-thumb you are breaking is don't skimp on buffer size. But, even if you do, you can handle things properly with fgets().
When you read a line from a file, fgets() (or POSIX getline()) read and include the '\n' as part of the buffer they fill (if there is room). If you are expecting up to 4-characters, then a buffer size of 5 is too-short-by-one to accommodate all your characters, the nul-terminating character, and the '\n'. Your circumstance attempting to read a 4-character line ("cats") with a 5-character buffer with fgets() would result in buf holding:
+---+---+---+---+---+
| c | a | t | s | \0| --> '\n' remains unread
+---+---+---+---+---+
You can gracefully handle that as well (but better not to skimp on buffer size) To gracefully handle the issue you need to check:
if '\n' is the last char in the buffer, complete line read, trim '\n' by overwriting with nul-terminating character;
otherwise, read next char;
if next char is '\n', then OK, you read all chars and there wasn't room for the '\n' which you just read and checked -- continue reading the next line;
else if next char is EOF, then you read all characters in the last line in a file with a non-POSIX end-of-file (no '\n' after the final line of data), break read loop you found EOF;
else additional character remain unread in the line, read and discard characters until the next '\n' or EOF is found
Putting that logic together, you could do:
#include <stdio.h>
#include <string.h>
int main (void) {
char buf[5];
while (fgets (buf, 5, stdin)) { /* read each line */
if (strchr (buf, '\n')) /* if '\n' found - line read */
buf[strcspn (buf, "\n")] = 0; /* nul-termiante at '\n' */
else { /* otherwise */
int c = getchar(); /* read next chars */
if (c == '\n') /* if '\n', OK read next line */
continue;
else if (c == EOF) /* if EOF, OK, non-POSIX eof */
break;
fputs ("error: line too long - discarding remainder.\n", stderr);
for (; c != '\n' && c != EOF; c = getchar()) {}
}
}
}
Look things over and let me know if you have further questions.
Here I made this function to read the file
char by char and returns only one line per call
so now you can read your file line by line, the
type Line has an array of chars value where we store
the line and an int hasNextLine 1 or 0 (bool)
that tell you if the file has another line or no,
this is handy when you loop over the file line by line.
#include <stdlib.h>
#include <stdio.h>
typedef struct {
char *value;
int hasNextLine;
} Line;
Line * getLine(FILE *file) {
Line *line = (Line *)malloc(sizeof(Line));
if(line == NULL) {
return NULL;
}
line->value = NULL;
line->hasNextLine = 1;
int n = 0, c;
while(1) {
c = getc(file);
char *tmpStr = (char *)realloc(line->value, n + 2);
if(tmpStr == NULL) {
line->hasNextLine = -1;
return line;
}
line->value = tmpStr;
if(c == EOF) {
line->hasNextLine = 0;
line->value[n] = '\0';
return line;
}
if(c == '\n') {
line->value[n] = '\0';
return line;
}
line->value[n] = c;
n++;
}
return line;
}
Usage:
// example reading one line
int main() {
FILE *f = fopen("your_file.txt", "r");
if(f == NULL) {
printf("File not found!");
return 1;
}
Line *l = getLine(f);
if(l != NULL) {
printf("%s\n", l->hasNextLine != -1 ? l->value :
"Error: while getting the line");
free(l->value);
free(l);
}
fclose(f);
return 0;
}
// example reading the whole file
int main() {
FILE *f = fopen("your_file.txt", "r");
if(f == NULL) {
printf("File not found!");
return 1;
}
Line *l;
int hasNextLine;
while(1) {
l = getLine(f);
if(l != NULL) {
printf("%s\n", l->hasNextLine != -1 ? l->value :
"Error: while getting the line");
free(l->value);
hasNextLine = l->hasNextLine;
free(l);
}
if(hasNextLine <= 0) {
break;
}
}
fclose(f);
return 0;
}
you can make a custom function for user input
char * sgetLine(char *msg) {
printf("%s", msg);
Line *l = getLine(stdin);
char *strLine = NULL;
if(l == NULL) {
return NULL;
}else {
if(l->hasNextLine == -1) {
free(l->value);
free(l);
return NULL;
}
strLine = l->value;
free(l);
return strLine;
}
}
so now you can use one function call to print
the question and to get the answer (char array)
int main() {
char *l = sgetLine("What is your name? ");
if(l != NULL) {
printf("%s\n", l);
}
free(l);
return 0;
}

Compare each line from two different files and print the lines that are different in C

Supposing that I have two files like this:
file1.txt
john
is
the new
guy
file2.txt
man
the old
is
rick
cat
dog
I'd like to compare first line from file1 with all the lines from file2 and verify if it exist. If not, go two the second line from file1 and compare it with all the lines from file2.. and so on until eof is reached by file1.
The output that I expect is:
john
the new
guy
How I thought this should be done:
read file1 and file2
create a function which returns the line number of each of them
take the first line from file1 and compare it to all the lines from file2
do this until all the lines from file1 are wasted
Now, I don't know what I'm doing wrong, but I don't get the result that I expect:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int countlines(char *filename)
{
int ch = 0, lines = 0;
FILE *fp = fopen(filename, "r");
if (fp == NULL)
return 0;
do {
ch = fgetc(fp);
if (ch == '\n')
lines++;
} while (ch != EOF);
if (ch != '\n' && lines != 0)
lines++;
fclose(fp);
return lines;
}
int main(int argc, char *argv[])
{
FILE *template_file = fopen(argv[1], "r");
FILE *data_file = fopen(argv[2], "r");
char buffer_line_template_file[100];
char buffer_line_data_file[100];
if (argc != 3)
{
perror("You didn't insert all the arguments!\n\n");
exit(EXIT_FAILURE);
}
if (template_file == NULL || data_file == NULL)
{
perror("Error while opening the file!\n\n");
exit(EXIT_FAILURE);
}
int counter = 0;
for (int i = 0; i < countlines(argv[1]); i++)
{
fgets(buffer_line_template_file, 100, template_file);
for (int j = 0; j < countlines(argv[2]); j++)
{
fgets(buffer_line_data_file, 100, data_file);
if (strcmp(buffer_line_template_file, buffer_line_data_file) != 0)
{
counter++;
printf("%d", counter);
}
}
}
printf("\n\n");
return 0;
}
Could someone please point me into the right direction ? For testing purposes I created a counter at the end which was a part of a small debug. There should be the print() function
As per #chux answer I got the following simplified code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *template_file = fopen(argv[1], "r");
FILE *data_file = fopen(argv[2], "r");
char buffer_line_template_file[100];
char buffer_line_data_file[100];
if (argc != 3)
{
perror("You didn't insert all the arguments!\n\n");
exit(EXIT_FAILURE);
}
if (template_file == NULL || data_file == NULL)
{
perror("Error while opening the file!\n\n");
exit(EXIT_FAILURE);
}
while(fgets(buffer_line_template_file, 100, template_file))
{
buffer_line_template_file[strcspn(buffer_line_template_file, "\n")] = '\0';
rewind(data_file);
while (fgets(buffer_line_data_file, 100, data_file))
{
buffer_line_data_file[strcspn(buffer_line_data_file, "\n")] = '\0';
if (strcmp(buffer_line_template_file, buffer_line_data_file) != 0)
{
printf("%s\n", buffer_line_template_file);
}
}
}
printf("\n\n");
return 0;
}
The above code is giving me the following output, which is not what is expected:
john
john
john
john
john
john
is
is
is
is
is
the new
the new
the new
the new
the new
the new
guy
guy
guy
guy
guy
guy
Problems with OP's code
Imprecise definition of line.
Excessive recalculation
Fuzzy determination of the number of lines in a file.
Unlike string, which has a precise definition in C, reading a line is not so well defined. The primary specificity issue: does a line contain the trailing '\n'. If the first answer is Yes, then does the last text in a file after a '\n' constitute a line? (Excessively long lines are another issue, but let us not deal with that today.)
Thus possibly some lines end with '\n' and others do not, fooling strcmp("dog", "dog\n").
The easiest solution is to read a line until either 1) a '\n' is encountered, 2) EOF occurs or 3) line buffer is full. Then after getting a line, lop off the potential trailing '\n'.
Now all lines code subsequently works with have no '\n'.
fgets(buffer_line_template_file, 100, template_file);
buffer_line_template_file[strcspn(buffer_line_template_file, "\n")] = '\0';
OP's loop is incredible wasteful. Consider a file with 1000 lines. Code will loop, calling 1000 times countlines() (each countlines() call reads 1000 lines) times when one countlines() call would suffice.
// for (int j = 0; j < countlines(argv[2]); j++)
int j_limit = countlines(argv[2]);
for (int j = 0; j < j_limit; j++)
There really is no need to count the line anyways, just continue until EOF (fgets() returns NULL). So no need to fix its fuzzy definition. (fuzzy-ness concerns same issues as #1)
int counter = 0;
for (fgets(buffer_line_template_file, 100, template_file)) {
buffer_line_template_file[strcspn(buffer_line_template_file, "\n")] = '\0';
rewind(data_file);
while ((fgets(buffer_line_data_file, 100, data_file)) {
buffer_line_data_file[strcspn(buffer_line_data_file, "\n")] = '\0';
if (strcmp(buffer_line_template_file, buffer_line_data_file) != 0) {
counter++;
printf("%d", counter);
}
}
}
Other simplifications possible - for another day.
FWIW, following counts lines of text allowing the last line in the file to optionally end with a '\n'.
unsigned long long FileLineCount(FILE *istream) {
unsigned long long LineCount = 0;
rewind(istream);
int previous = '\n';
int ch;
while ((ch = fgetc(inf)) != EOF) {
if (previous == '\n') LineCount++;
previous = ch;
}
return LineCount;
}
Note that this function may get a different result that fgets() calls. Consider a file of one line of 150 characters. fgets(..., 100,...) will report 2 lines. FileLineCount() reports 1.
[Edit] Updated code to conform to OP functionality.
int found = 0;
while (fgets(buffer_line_data_file, 100, data_file))
{
buffer_line_data_file[strcspn(buffer_line_data_file, "\n")] = '\0';
if (strcmp(buffer_line_template_file, buffer_line_data_file) == 0)
{
found = 1;
break;
}
}
if (!found) printf("%s\n", buffer_line_template_file);
This program prints the diff of two files file1.txt and file2.txt.
#include<stdio.h>
#include <stdlib.h>
#include <memory.h>
int main() {
FILE *fp1, *fp2;
int ch1, ch2;
char fname1[40], fname2[40];
char *line = NULL;
size_t len = 0;
ssize_t read;
char *line2 = NULL;
size_t len2 = 0;
ssize_t read2;
fp1 = fopen("file1.txt", "r");
fp2 = fopen("file2.txt", "r");
if (fp1 == NULL) {
printf("Cannot open %s for reading ", fname1);
exit(1);
} else if (fp2 == NULL) {
printf("Cannot open %s for reading ", fname2);
exit(1);
} else {
while ((read = getline(&line, &len, fp1)) != -1 && (read2 = getline(&line2, &len2, fp2)) != -1) {
if (!strcmp(line, line2)) {
printf("Retrieved diff on line %zu :\n", read);
printf("%s", line);
}
}
if (ch1 == ch2)
printf("Files are identical \n");
else if (ch1 != ch2)
printf("Files are Not identical \n");
fclose(fp1);
fclose(fp2);
}
return (0);
}
You already have a very good answer (and always will from chux), but here is a slightly different approach to the problem. It uses automatic storage to reading file2 into an array of strings and then compares each line in file1 against every line in file2 to determine whether it is unique. You can easily convert the code to dynamically allocate memory, but for sake of complexity that was omitted:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { MAXC = 256, MAXL = 512 };
void file1infile2 (FILE *fp2, FILE *fp1, size_t *n2, size_t *n1);
int main (int argc, char **argv) {
FILE *fp1 = fopen (argc > 1 ? argv[1] : "file1.txt", "r");
FILE *fp2 = fopen (argc > 2 ? argv[2] : "file2.txt", "r");
size_t n1 = 0, n2 = 0;
if (!fp1 || !fp2) {
fprintf (stderr, "error: file open failed.\n");
return 1;
}
printf ("\nunique words in file1, not in file 2.\n\n");
file1infile2 (fp2, fp1, &n2, &n1);
printf ("\nanalyzed %zu lines in file1 against %zu lines in file2.\n\n",
n1, n2);
return 0;
}
void file1infile2 (FILE *fp2, FILE *fp1, size_t *n2, size_t *n1)
{
char buf[MAXC] = "";
char f2buf[MAXL][MAXC] = { "" };
size_t i;
*n1 = *n2 = 0;
while (*n2 < MAXL && fgets (buf, MAXC, fp2)) {
char *np = 0;
if (!(np = strchr (buf, '\n'))) {
fprintf (stderr, "error: line exceeds MAXC chars.\n");
exit (EXIT_FAILURE);
}
*np = 0;
strcpy (f2buf[(*n2)++], buf);
}
while (*n1 < MAXL && fgets (buf, MAXC, fp1)) {
char *np = 0;
if (!(np = strchr (buf, '\n'))) {
fprintf (stderr, "error: line exceeds MAXC chars.\n");
exit (EXIT_FAILURE);
}
*np = 0, (*n1)++;
for (i = 0; i < *n2; i++)
if (!(strcmp (f2buf[i], buf)))
goto matched;
printf (" %s\n", buf);
matched:;
}
}
Look over the code and let me know if you have any questions.
Example Use/Output
$ ./bin/f1inf2 dat/f1 dat/f2
unique words in file1, not in file 2.
john
the new
guy
analyzed 4 lines in file1 against 6 lines in file2.

Read file word by word and output WITH white spaces

The input text file is like so:
Hello my
name is
mark.
and
im
going
to
love
c!
Code:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]){
FILE *pFile;
char x[60];
pFile = fopen("test0.txt","r");
if(pFile != NULL){
while(fscanf(pFile, " %60s", x) == 1){
printf("%s",x);
}
}
}
Output text file is:
Hellomynameismark.andimgoingtolovec!
I want the Output to be like:
Hello my name is mark. and im going to love c!
Very new C programmer so only know the basics.
Edit----
int main(int argc, char *argv[]){
FILE *pFile;
char x[60],line[60];
pFile = fopen("test0.txt","r");
while(!feof(pFile)){
fgets(line, 60, pFile);
strtok(line, "\r\n");
printf("%s", line );
}
fclose(pFile);
Output:
Hello myname is mark.andim goingtolovec!
This does not leave spaces between new lines. However if I take out the strtok line the output will be like this:
Hello my
name is
mark.
and
im
going
to
love
c!
--Edit
.sp 2
.ce
This is an example file for formatting.
.sp 1
The above line
was formatted using a .ce 1 command, which means 'centre
the following line',
so it should appear in the
centre of the page.
The paragraph was separated from the heading using
a .sp 1 command to create a single blank line.
There should also be two blank lines above the centred heading to make reading it slightly easier.
The simple answer is:
while(fscanf(pFile, " %59[^\n]%*c", x) == 1)
Here %[^\n] uses the character class [stuff] to read everything up to the newline. %*c simply reads and discards the newline without adding it to the match count for fscanf.
However for line-oriented input, you should really use one of the line-oriented functions provided by the standard library (e.g. fgets or POSIX getline).
Using fgets & strtok
As you have taken from the comment, the use of feof is going to cause you nothing but grief. You will want to simply use the return of fgets to determine end of file. Here is an example that puts all the pieces of the puzzle together:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXWDS 20
#define MAXCHR 60
int main (int argc, char **argv) {
char line[MAXCHR] = {0};
char *words[MAXWDS] = {NULL};
FILE *pFile = NULL;
size_t i, index = 0;
/* open file for reading (if provided), or read from stdin */
if (!(pFile = argc > 1 ? fopen (argv[1], "r") : stdin)) {
fprintf (stderr, "error: file open failed '%s'.\n", argv[1]);
return 1;
}
while (fgets (line, 60, pFile))
{
char *p = line;
/* split line into tokens, stored in words[] */
for (p = strtok (p, " \r\n"); p; p = strtok (NULL, " \r\n")) {
words[index++] = strdup (p); /* allocate & copy */
if (index == MAXWDS) /* check pointer limit */
break;
}
}
if (pFile != stdin) fclose (pFile);
/* output in a single line */
for (i = 0; i < index; i++) {
printf (" %s", words[i]);
free (words[i]); /* free allocated memory */
}
putchar ('\n');
return 0;
}
Compile
gcc -Wall -Wextra -o bin/fgets_strtok fgets_strtok.c
Output
$ ./bin/fgets_strtok dat/hellomark.txt
Hello my name is mark. and im going to love c!
Note: to simply print the line out with spaces between the words, as long as there is already a space between each of the words in each line, there is no reason to go to the trouble to separate each line into individual words, you can simply print the contents of each line out in a space separate fashion. The only issue you run into using fgets is that it will also read the newline (or carriage return, newline) as part of the string. That is simple to remove. You can replace the entire read loop with:
while (fgets (line, 60, pFile))
{
size_t len = strlen (line);
/* strip trailing newline (or carriage return newline ) */
while (len && (line[len-1] == '\n' || line[len-1] == '\r'))
line[--len] = 0; /* overwrite with null-terminating char */
words[index++] = strdup (line); /* allocate & copy */
if (index == MAXWDS) /* check pointer limit */
break;
}
Output
$ ./bin/fgets_mark <dat/hellomark.txt
Hello my name is mark. and im going to love c!
Standard Way to Read from File Only (not File or stdin)
I apologize for the getting ahead of you a bit by including a way to either open a file (if provided on the command line) or read from stdin (if no filename was provided). The standard way is to first check that the correct number of arguments were provided on the command line, and then open the filename provided, validate it is open, and then process input. What I did was throw a ternary operator into the fopen command that said.
pFile = argc > 1 ? fopen (argv[1], "r") : stdin
The right side of the '=' sign is a ternary operator, which is simply a shorthand for if -> then -> else. What it does is ask is argc > 1? If that tests true, then pFile = fopen (argv[1], "r");. If argc > 1 tests false, then pFile = stdin;
See if the standard way makes more sense:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXWDS 20
#define MAXCHR 60
int main (int argc, char **argv) {
char line[MAXCHR] = {0};
char *words[MAXWDS] = {NULL};
FILE *pFile = NULL;
size_t i, index = 0;
/* validate sufficient input */
if (argc < 2 ) {
fprintf (stderr, "error: insufficient input, usage: %s filename\n", argv[0]);
return 1;
}
/* open file provided on command line for reading */
pFile = fopen (argv[1], "r");
if (!pFile) {
fprintf (stderr, "error: file open failed '%s'.\n", argv[1]);
return 1;
}
while (fgets (line, 60, pFile)) /* read each line in file */
{
size_t len = strlen (line);
/* strip trailing newline (or carriage return newline ) */
while (len && (line[len-1] == '\n' || line[len-1] == '\r'))
line[--len] = 0; /* overwrite with null-terminating char */
words[index++] = strdup (line); /* allocate & copy */
if (index == MAXWDS) /* check pointer limit */
break;
}
if (pFile != stdin) fclose (pFile);
/* output in a single line */
for (i = 0; i < index; i++) {
printf (" %s", words[i]);
free (words[i]); /* free allocated memory */
}
putchar ('\n');
return 0;
}
A simple state machine does the trick - no line length limitation.
#include <stdio.h>
int main(void) {
FILE *pFile = fopen("test0.txt","r");
if(pFile != NULL) {
int previous_isspace = 0;
int ch;
for (;;) {
ch = fgetc(pFile);
if (ch == EOF) break;
if (isspace(ch)) {
previous_isspace = 1;
} else {
if (previous_isspace == 1) {
fputc(' ', stdout);
}
previous_isspace = 0;
fputc(ch, stdout);
}
}
fclose(pFile);
fputc('\n', stdout); // If code should have a \n at the end
}
}
I think, It is sufficient take a look am i miss anything.
if(pFile != NULL){
// while(fscanf(pFile, " %60s", x) == 1){
while (fgets(x, sizeof(x), pFile) != NULL) {
token = strtok(x,"\r\n");
if(token != NULL)
printf("%s ",x);
else
printf("%s",x);
}
fclose(pFile);
}

copy content of file in reverse order

I need to write a program that is copying the content of a file to another file and reverses it.
I found an example and read it through to understand what is going on.
The problem is that my program has to use two functions:
void reverse(char line[]){
int i;
int length;
char tmp;
..
..
..
return;
}
(no further paramters or local variables)
The second function does the rest of the work(opens files, copies files, closes files)
The main program only reads the name of the files and calls the copy function.
#include<stdio.h>
#include<string.h>
void reverse(char line[])
{
int i;
int length;
char temp;
if (line == NULL)
return;
length = strlen(line);
for (i = 0 ; i < length / 2 + length % 2 ; ++i)
{
if (line[i] == line[length - i - 1])
continue;
temp = line[i];
line[i] = line[length - i - 1];
line[length - i - 1] = temp;
}
return;
}
int main()
{
FILE *src_fh, *dst_fh;
char src_fn[256+1], dst_fn[256+1];
printf("Enter Source File Name:\n");
fgets(src_fn, sizeof(src_fn), stdin); reverse(src_fn);
if( (src_fh = fopen(src_fn, "r")) == NULL )
{
printf("ERROR: Source File %s Failed To Open...\n",src_fn);
return(-1);
}
printf("Enter Destination File Name:\n");
fgets(dst_fn, sizeof(dst_fn), stdin); reverse(dst_fn);
if( (dst_fh = fopen(dst_fn, "w+")) == NULL )
{
fclose(src_fh);
printf("ERROR: Destination File %s Failed To Open...\n",dst_fn);
return(-2);
}
int ch;
while( (ch = fgetc(src_fh)) != EOF )
{
fputc(ch, dst_fh);
}
fclose(src_fh);
fclose(dst_fh);
return 0;
}
You only need to swap the first character with the last, the second with the pre-last, and so on.
You actually don't need the int temp variable, but since it seems to be required, here it is
void reverse(char line[])
{
int i;
int length;
char temp;
if (line == NULL)
return;
length = strlen(line);
for (i = 0 ; i < length / 2 + length % 2 ; ++i)
{
if (line[i] == line[length - i - 1])
continue;
temp = line[i];
line[i] = line[length - i - 1];
line[length - i - 1] = temp;
}
return;
}
This is an improved version, without int temp, instead we store the result of length / 2 + length % 2 so it's not recalculated on each iteration
void reverse(char line[])
{
int i;
int length;
int half;
if (line == NULL)
return;
length = strlen(line);
half = length / 2 + length % 2;
for (i = 0 ; i < half ; ++i)
{
if (line[i] == line[length - i - 1])
continue;
line[length] = line[i];
line[i] = line[length - i - 1];
line[length - i - 1] = line[length];
}
line[length] = '\0';
return;
}
just use the location of the terminating '\0' byte as the temp when swapping.
For the second function, read each line using fgets and write it to the file with fprintf, just remember to remove the newline character from the read strings, you can use the chomp function y posted for that, if you don't remove the newline, the reversed lines will have the newline at the beginning of the line.
The prameter name line in the prototype void reverse(char line[]) seems to give a hint, how the given exercise might be intended to be solved.
split the file in lines
reverse every line
reverse the order of the lines
Nevertheless you should watch out following this strategy, as there is still a really nasty gotcha involved, if your file may contain any data.
In this case you'll get in big trouble finding the end of line[] as '\0' termination might get confused with a literal '\0' in the line.
As a workaround you might try to replace any literal occurance of '/0' by the sequence '\0' 'x' and mark the end of your line by the sequence '\0' '-' or whatever before passing it to reverse() and reaversing the substitution after writing the reversed line to the file.
Unfortunately this attempt doesn't look too elegant, but maybe reversing a file the way it is meant to be done in the exercise isn't really elegant anyays.
the following code
1) incorporates proper error checking
2) outputs each input line, reversed, to the output file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* chomp(char* p)
{
int len;
if(!p) return(p);
if( (len=strlen(p))<=0 ) return(p);
if( p[len-1] == '\n' ) { p[--len] = '\0'; }
if( p[len-1] == '\r' ) { p[--len] = '\0'; }
return(p);
} // end function: chomp
int main()
{
/* Create Usable Variables */
FILE *src_fh = NULL;
FILE *dst_fh = NULL;
char src_fn[256+1] = {'\0'};
char dst_fn[256+1] = {'\0'};
char line[2048] = {'\0'};
/* Retrieve Source File Name From User */
printf("Enter Source File Name:\n");
if( NULL == (fgets(src_fn, sizeof(src_fn), stdin) ) )
{ // fgets failed
perror("fgets for input file name failed" );
exit(EXIT_FAILURE);
}
// implied else, fgets successful
chomp(src_fn); // remove trailing newline characters
/* Attempt Opening Source File For Reading */
if( (src_fh = fopen(src_fn, "r")) == NULL )
{
perror( "fopen failed" );
printf("ERROR: Source File %s Failed To Open...\n",src_fn);
return(-1);
}
// implied else, fopen source file successful
/* Retrieve Destination File Name From User */
printf("Enter Destination File Name:\n");
if( NULL == (fgets(dst_fn, sizeof(dst_fn), stdin) ) )
{ // then fgets failed
perror( "fgets for output file name failed" );
fclose(src_fh); // cleanup
exit( EXIT_FAILURE );
}
// implied else, fgets for output file name successful
chomp(dst_fn); // remove trailing newline characters
/* Attempt Opening Destination File For Writing */
if( NULL == (dst_fh = fopen(dst_fn, "w")) )
{
perror( "fopen for output file failed" );
fclose(src_fh); // cleanup
printf("ERROR: Destination File %s Failed To Open...\n",dst_fn);
return(-2);
}
// implied else, fopen for output file successful
int index;
/* Copy Source File Contents (reversed, line by line) to destination file */
while( NULL != (fgets(line, sizeof(line), src_fh) ) )
{
chomp(line); // remove trailing newline characters
index = strlen(line) - 1; // -1 because arrays start with offset 0
// and strlen returns offset to '\0'
// output reversed line to file
while( index >= 0 )
{
fputc( line[index], dst_fh );
index--;
} // end while
fputc( '\n', dst_fh );
} // end while
/* Close Files On Success */
fclose(src_fh);
fclose(dst_fh);
return 0;
} // end function: main

File reading in C. Complications with fread function

I'm reading an input file and I am having complications regarding reading each line in it. My input file has two types of records. One has 52 characters while the other has 926. I don't know what to do with my fread function so that it can handle both records. Can anyone help me out? thanks
#define LINESZ 927 /* one extra byte for new line */
int num; /* integer for line number */
char buffer[LINESZ]; /* buffer for file read line */
int main()
{
FILE *ifp, *ofp;
ifp = fopen("dd:INPUTF", "r");
ofp = fopen("dd:OUTPUTF", "w");
while (!feof(ifp)) {
if (num = (fread(buffer, sizeof(char), LINESZ, ifp))) {
if (buffer[22] == 'O') {
printf("ravroot, %c\n", buffer[22]);
printf("%s*\n", buffer);
}
else if (buffer[22] == 'A') {
printf("ravrate, %c\n", buffer[22]);
printf("%s*\n", buffer);
}
}
}
fclose(ifp);
fclose(ofp);
return(0);
}
When reading lines from a file, you should use the fgets function. Note however, that fgets will write the newline character to your buffer, so you need to strip the newline out. The resulting code looks like this
#define LINESZ 1024 /* lots of extra bytes, memory is cheap */
char buffer[LINESZ]; /* buffer for file read line */
int main( void )
{
int length;
FILE *ifp, *ofp;
ifp = fopen("dd:INPUTF", "r");
ofp = fopen("dd:OUTPUTF","w");
while( fgets( buffer, LINESZ, ifp ) != NULL )
{
// remove the newline character, if any
length = strlen( buffer );
if ( length > 0 && buffer[length-1] == '\n' )
buffer[--length] = '\0';
if ( length > 22 )
{
if(buffer[22] == 'O')
{
printf("ravroot, %c\n", buffer[22]);
printf("%s*\n", buffer);
}
else if(buffer[22] == 'A')
{
printf("ravrate, %c\n", buffer[22]);
printf("%s*\n", buffer);
}
}
}
fclose(ifp);
fclose(ofp);
return(0);
}
If every record is in seperate line thne use the fgets function which will stop when the newline is encountered , eg:
while(fgets(buf,LINESZ,ifp) != NULL)
{
//you can put your code here
}

Resources