Issues in string manipulation - c

The log.txt file consist of some data. The program looks for ":" and when it find it prints "Done". The program compiles successfully but never prints "Done".
char *atrbt ;
FILE *fp;
int i = 0;
if (fp = fopen("log.txt", "r+")) {
while (fscanf(fp, "%c", &atrbt) != EOF) {
printf("%c", atrbt);
if(atrbt[i] == ':') { <------------ Issue
printf("Done");
}
++i;
}
}

You are mixing between char and char pointers. One of the possible correct ways could be (code is untested):
char atrbt;
FILE *fp;
if (fp = fopen("log.txt", "r+")) {
while ((atrbt = getc(fp)) != EOF) {
printf("%c", atrbt);
if(atrbt == ':') {
printf("Done");
}
}
}

Related

How to merge the contents of two files into a new file, appearing side by side by line?

I've been trying to merge the contents of two .txt files into a third .txt file that combines the output. All I know how to do (and all I have been able to find answers for), however, is to merge them by putting the contents of the first file first, and the second file second. However, I would prefer the output to list the first line of the first file, then the first line of the second file -- followed on a new line by the second line of the first file and the second line of the second file.
To make this clearer visually, the code is currently appearing as:
file1-line1
file1-line2
file1-line3
file2-line1
file2-line2
file2-line3
... When I'd like it to appear as:
file1-line1 file2-line1
file1-line2 file2-line2
file1-line3 file2-line3
The code I have is very basic and executes the first example fine:
int main()
{
FILE *pointer1 = fopen("file1.txt", "r");
FILE *pointer2 = fopen("file2.txt", "r");
FILE *pointer3 = fopen("combined.txt", "w");
int ch;
if (pointer1 == NULL || pointer2 == NULL || pointer3 == NULL)
{
puts("Could not open files");
exit(0);
}
while ((ch = fgetc(pointer1)) != EOF)
fputc(ch, pointer3);
while ((ch = fgetc(pointer2)) != EOF)
fputc(ch, pointer3);
printf("Merged file1.txt and file2.txt into combined.txt");
fclose(pointer1);
fclose(pointer2);
fclose(pointer3);
return 0;
}
Is there a way to output the described situation? I am aware that E0F refers to the end of a file, and is likely causing an issue. Is there a similar condition for an end of a line (like E0L)?
Edit: Changed char ch to int ch.
First, if you have a Unix-like system, the paste command already does that. Next as you want to process lines, you should use fgets. Here you have to loop over input files one line at a time, copy the lines to the output file without the newline, and add the new line after copying everything.
As the processing for both input files is the same, and as I am lazy, I wrote a function to only write it once. In the end code could be:
FILE *copyline(FILE *in, FILE *out) {
char line[256];
if (in != NULL) {
for (;;) { // loop if the line is larger that sizeof(line)
if (NULL == fgets(line, sizeof(line), in)) { // EOF on file1
fclose(in);
in = NULL;
break;
}
size_t end = strcspn(line, "\n");
if (end != 0) fwrite(line, 1, end, out); // smth to write
if (end != strlen(line)) break; // \n found: exit loop
}
}
return in;
}
int main()
{
FILE *pointer1 = fopen("file1.txt", "r");
FILE *pointer2 = fopen("file2.txt", "r");
FILE *pointer3 = fopen("combined.txt", "w");
const char sep[] = " "; // a separator between lines of both file
if (pointer1 == NULL || pointer2 == NULL || pointer3 == NULL)
{
puts("Could not open files");
exit(0);
}
for (;;) {
pointer1 = copyline(pointer1, pointer3);
fwrite(sep, strlen(sep), 1, pointer3);
pointer2 = copyline(pointer2, pointer3);
if (pointer1 == NULL && pointer2 == NULL) break;
fputc('\n', pointer3); // if smth was written, add a newline
printf(".");
}
printf("Merged file1.txt and file2.txt into combined.txt");
fclose(pointer3);
return 0;
}
Here's one way to approach it:
#include <err.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
FILE *
xfopen(const char *path, const char *mode)
{
FILE *fp = path[0] != '-' || path[1] != '\0' ? fopen(path, mode) :
*mode == 'r' ? stdin : stdout;
if( fp == NULL ) {
perror(path);
exit(EXIT_FAILURE);
}
return fp;
}
int
main(int argc, char **argv)
{
if( argc < 3 ) {
printf("usage: %s file1 file2\n", basename(argv[0]));
}
FILE *pointer1 = xfopen(argv[1], "r");
FILE *pointer2 = xfopen(argv[2], "r");
FILE *current = pointer1;
int ch;
while( ( ch = fgetc(current)) != EOF ) {
if( ch == '\n' ) {
if( current == pointer1 ) {
int k;
current = pointer2;
if( (k = fgetc(current)) != EOF ) {
ungetc(k, current);
ch = ' ';
}
} else {
current = pointer1;
}
}
putchar(ch);
}
if( ferror(current) ) {
err(EXIT_FAILURE, "Error reading %s",
current == pointer1 ? argv[1] : argv[2]);
}
current = current == pointer1 ? pointer2 : pointer1;
while( (ch = fgetc(current)) != EOF) {
putchar(ch);
}
fclose(pointer1);
fclose(pointer2);
return 0;
}
#include <stdio.h>
int main()
{
FILE *pointer1 = fopen("file1.txt", "r");
FILE *pointer2 = fopen("file2.txt", "r");
FILE *pointer3 = fopen("combined.txt", "w");
char ch1, ch2;
if (pointer1 == NULL || pointer2 == NULL || pointer3 == NULL)
{
puts("Could not open files");
return 0;
}
do
{
char c1 = fgetc(pointer1);
char c2 = fgetc(pointer2);
if (feof(pointer1) || feof(pointer2))
break;
while(c1!='\n')
{
fputc(c1,pointer3);
c1=fgetc(pointer1);
if(feof(pointer1)) break;
}
fputc(' ',pointer3);
while(c2!='\n')
{
fputc(c2,pointer3);
c2=fgetc(pointer2);
if(feof(pointer2)) break;
}
fputc('\n',pointer3);
} while (1);
printf("Merged file1.txt and file2.txt into combined.txt");
fclose(pointer1);
fclose(pointer2);
fclose(pointer3);
return 0;
}
This works like you want.
Output: Combined file.txt
file1-line1 file2-line1
file1-line2 file2-line2
file1-line3 file2-line3

Error Segmentation Fault Core Dumped in C - Ubuntu

I am facing this error. I have searched on Internet and that solution is passing two arguments in main, argc and argv. I dont know why to use it and how i use it?
My program is to read a file that contains integers and print them
#include<stdio.h>
int main()
{
int no;
char ch;
FILE *ftr;
ftr = fopen("numbers.txt", "r");
while ((ch = fgetc(ftr)) != EOF)
{
no = ch - '0';
printf("%d", no);
}
fclose(ftr);
return 0;
}
The only explanation for this is that ftr == NULL, try
#include <stdio.h>
int main(void)
{
FILE *file;
char chr;
file = fopen("numbers.txt", "r");
if (file == NULL) {
fprintf(stderr, "cannot open the file\n");
return -1;
}
while ((chr = fgetc(file)) != EOF) {
fprintf(stdout, "%d", chr - '0');
}
fclose(file);
return 0;
}
Ideally you should check file opening and close by using the same level of checking as employed by these two snippets.
f = fopen(filename,"r");
if (f == NULL) {
fprntf(stderr, "fopen failed: %s", strerror(errno));
// and terminate the file IO here
}
..
..
if (fclose(f) == EOF) {
fprint(stderr, "fclose failed");
}

How to read line wise from a file in c (Ubuntu)?

I'm trying to extract string line by line from a file but its giving me no output.The file has some words in a token wise.Here is my code :
#include<stdio.h>
#include<string.h>
main(void)
{
FILE *open;
open = fopen("assembly.txt", "r");
FILE *write;
write = fopen("tokens.txt", "w");
FILE *a;
char ch;
char *str;
while ((ch = fgetc(open)) != EOF)
{
if (ch == 32 || ch == ',' || ch == '#')
fprintf(write, "\n");
else
fprintf(write, "%c", ch);
}
a = fopen("tokens.txt", "r");
while (!feof(a))
{
if (fgets(str, 126, a))
printf("%s", str);
}
}
I'm getting no output at all.The program executes successfully without any output!
There are several errors in your code.
First: you didn't close the files.
Second: you did a fgets with str which has not been allocated which will segfault.
With the fixes, now your code is:
#include<stdio.h>
#include<string.h>
int main(void)
{
FILE *open;
open = fopen("assembly.txt", "r");
FILE *write;
write = fopen("tokens.txt", "w");
FILE *a;
char ch;
char str[127];
while ((ch = fgetc(open)) != EOF)
{
if (ch == 32 || ch == ',' || ch == '#')
fprintf(write, "\n");
else
fprintf(write, "%c", ch);
}
fclose(write);
fclose(open);
a = fopen("tokens.txt", "r");
while (!feof(a))
{
if (fgets(str, 126, a))
printf("%s", str);
}
fclose(a);
return 0;
}

Change text file case and copy data in C

I am creating a program which copies text file data from one file and changes its case to lower or upper on choice, but when I execute the program I receive the following result and the text case is not changed or copied.
#include <stdio.h>
int main() {
FILE *fp = NULL;
FILE *fp2 = NULL;
char str[200];
char var;
int i;
char copy;
fp = fopen("file1.txt", "a");
fp2 = fopen("file2.txt", "w");
printf("Enter choice: ");
scanf(" %c", &var);
if (fp != NULL && var == 'L') {
while ( fgets(str, 200, fp) != NULL ) {
putchar(tolower(fp[i]));
i++;
}
puts(str);
}
else if (fp != NULL && var == 'U') {
while ( fgets(str, 200, fp) != NULL ) {
putchar(toupper(fp[i]));
i++;
}
puts(str);
}
else {
printf("ERROR: No proper choice was made \n");
}
while (1) {
copy = fgetc(fp);
if (copy == EOF) {
break;
}
else {
putc(copy , fp2);
}
}
return 0;
fclose(fp);
fclose(fp2);
}
The output I get is:
Enter choice: U
▒▒
putchar(tolower(fp[i])); should have generated compiler warnings. Insure the compiler warning are all enabled or consider a new compiler.
Replace in 2 places, both upper and lower section.
while ( fgets(str, 200, fp) != NULL ) {
putchar(tolower(fp[i]));
i++;
}
// With to iterate over each character
while (fgets(str, sizeof str, fp) != NULL) {
for (i=0; str[i]; i++) {
putchar(tolower(str[i]));
}
}
Change to int to properly distinguish all characters returned from fgetc() from EOF.
Open with "r" to read from the beginning of the file
// fp = fopen("file1.txt", "a")
fp = fopen("file1.txt", "r");
The problem is that you use fgets, which reads a string from the file. A better way of doing this would be to use fgetc, which reads just one character at a time and process it appropiately.

How to write text from file to a string in C

I want to write code were the user is asked to write the name of a file. Then I want to analyze the file's content for a symbol, let's say 'e'.
My problem is that I don't know how to start analyzing the file the correct way so that the content can be checked.
int main() {
char c[1000], file_name[1000];
int i;
int s = 0;
FILE *fp;
printf("Enter the name of file you wish to see\n");
gets(file_name);
if ((fp = fopen(file_name, "r")) == NULL){
printf("Error! opening file");
exit(1);
}
if (fp) {
while (fscanf(fp, "%s", c) != EOF) {
printf("%s", c);
}
fclose(fp);
for (i = 0; c[i] != '\0'; ++i) {
puts(c);
if (c[i] == 'e') {
++s;
}
}
printf("\nWhite spaces: %d", s);
_getche();
return 0;
}
}
char line[512]; /*To fetch a line from file maximum of 512 char*/
rewind(fp);
memset(line,0,sizeof(line)); /*Initialize to NULL*/
while ( fgets(line, 512, fp ) && fp !=EOF)
{
/*Suppose u want to analyze string "WELL_DONE" in this fetched line.*/
if(strstr(line,"WELL_DONE")!=NULL)
{
printf("\nFOUND KEYWOD!!\n");
}
memset(line,0,sizeof(line)); /*Initialize to null to fetch again*/
}
If its just a symbol you're looking for, or a char, you can simply use getc() :
int c;
....
if (fp) {
while ((c = getc(fp)) != EOF) {
if (c == 'e') {
// Do what you need
}
}
Or, alternatively, if it's a word you're looking for, fscanf() will do the job:
int c;
char symb[100];
char symbToFind[] = "watever"; // This is the word you're looking for
....
while ((c = fscanf(fp, %s, symb)) != EOF) {
if (strcmp(symb, symbToFind) == 0) { // strcmp will compare every word in the file
// do whatever // to symbToFind
}
}
These alternatives will allow you to search every char or string in the file, without having to save them as an array.

Resources