Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How do I convert this program(binary.c) into a command line application so it can read any binary text (e.g. binary1.txt) file and then convert it to ASCII? For example I want to do:
./binary < binary1.txt
and it should print
Hello World!
at the command line.
This is my program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *f_ptr;
f_ptr = fopen("binary1.txt", "r");
if (f_ptr == NULL){
printf("Error opening binary1.txt");
return 1;
}
while(1){
int ch = fgetc(f_ptr);
if(ch == EOF)
break;
printf("%c",ch);
}
fclose(f_ptr);
return 0;
}
I am not sure what you are asking. I guess you need to compile your source using a compiler. If you are using Linux you could do:gcc binary.c -o binary to get an executable file...
#include <stdio.h>
int main(int argc, char *argv[]){
FILE *fp;
if(argc < 2)//./binary < binary1.txt
fp = stdin;
else {//./binary binary1.txt
if((fp=fopen(argv[1], "r")) == NULL){
perror("fopen");
return 1;
}
}
int ch, count = 0;
unsigned char out = 0;
while((ch = fgetc(fp))!=EOF){
if(ch == '1' || ch == '0'){
out <<= 1;
out += ch - '0';
if(++count == 7){//7bit code
printf("%c", out);
count = out = 0;
}
}
}
puts("");
fclose(fp);
return 0;
}
From what I understand you are trying to pass the filename to your program and not sure how to do that. In that case you might want to do this
int main(int argc, char* argv[])
{
if(argc>1)
std::string filename = argv[1];
//now you have the file in the filename
// your code
return 0;
}
so when you execute just do this
./binary binary1.txt
you don't need to use < and I am assuming that both your executable and text file are in the same directory
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am confused that how to give break line after every word which is present in a file.
Words in text file
Name Date of birth <---- I put this in code
John 02\02\1999 <---- I want to jump to this line
I want this
Here is your: Name
Here is your: Date of Birth
But it is giving me this
Here is your: N
Here is your: a
Here is your: m
Here is your: e
And I don't know how to get it.
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE * fr = fopen("/home/bilal/Documents/file.txt","r");
char ch;
if(fr != NULL){
while(!feof(fr)){
ch = fgetc(fr);
printf("Here is your %c\n: ", ch);
}
fclose(fr);
}
else{
printf("Unable to read file.");
}
return 0;
}
Within your while loop instead of immediately printing the character that you read, store the char in a char array. Add an if statement that does a comparison that checks if the read char is a space character. If it is you should print the stored array and set the index of the array back to 0.
Example:
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE * fr = fopen("file.txt","r");
char ch[100];
int index = 0;
if(fr != NULL){
while((ch[index] = fgetc(fr)) != EOF){
//printf("%c\n", ch[index]);
if(ch[index] == ' ') {
ch[index] = '\0';
printf("Here is your: %s\n", ch);
index = 0;
}
else {
index++;
}
}
fclose(fr);
}
else{
printf("Unable to read file.");
}
return 0;
}
Based on the line of text of the file provided we can assume that if the first letter of the word is in uppercase then it is the start of the next sentence:
Name Date of birth ID card number Phone number Address Account Fixing year
And use this to divide the line into sentences.
So here is the code by Christopher changed to group the words into sentences:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(){
FILE * fr = fopen("file.txt","r");
char ch[100];
int index = 0;
if(fr != NULL){
while((ch[index] = fgetc(fr)) != EOF){
if(index > 0 && ch[index-1] == ' ' && isupper(ch[index])) {
ch[index-1] = '\0';
printf("Here is your: %s\n", ch);
ch[0] = ch[index];
index = 1;
}
else {
index++;
}
}
ch[index] = '\0';
printf("Here is your: %s\n", ch);
fclose(fr);
}
else{
printf("Unable to read file.");
}
return 0;
}
I have the following head file code and I am looking for optimization.
How i can rewrite the code using POSIX functions without user-space buffering, without reading character by character but fixed lengths? This I think will improve the efficiency of the code.
#include "stdio.h"
int main(int argc, char* argv[])
{
if(argc<2)
{
printf("<sintaxa(%s)> fisier \n",argv[0]);
}
else
{
int count = 0;
FILE * file;
if((file = fopen(argv[1],"r")) == NULL )
printf(" file %s open error\n",argv[0]);
fseek(file,0,SEEK_SET);
while(count < 10)
{
fseek(file,-2,SEEK_SET);
if(ftell(file)<0L)
break;
char now = fgetc(file);
printf("%c",now);
if(now == '\n')
++count;
}
fclose(file);
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am working on a project. there is a .txt file that includes negative integers like this
0 -1 0
-1 20 -1
0 -1 0
My problem is I couldn't read negative numbers. What is your suggestions for this problem?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char * argv[]) {
FILE *fptr;
char letter;
fptr = fopen(argv[1],"r");
if(fptr == NULL){
printf("Please provide an argument\n");
exit(1);
}
while ( ( letter = fgetc(fptr) ) != EOF ) {
printf("%c",letter);
}
printf("\n");
fclose(fptr);
return 0;
}
You can try this to read the lines from the file and then parse your numbers.
getline reads a line from your file and strtok then gives you a string until the specified delimeter (space in your case). Hope this helps.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char * argv[]) {
FILE * fptr = fopen(argv[1],"r");;
char * line = NULL;
size_t lineSize = 0;
if(fptr == NULL){
printf("Please provide an argument\n");
exit(1);
}
while(getline(&line, &lineSize, fptr) != -1) {
line[strcspn(line, "\n")] = 0;
printf("%s\n", line);
char * token = strtok(line, " ");
printf("%s\n", token);
while((token = strtok(NULL, " ")) != NULL) {
printf("%s\n", token);
}
}
fclose(fptr);
return 0;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I tried to read 2 binary file and check if one file has at least the content of the second file(Not necessarily the same completely).
What I had tried:
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *file1, *file2;
file1 = fopen(argv[1], "rb");
file2 = fopen(argv[2], "rb");
if(file1 == NULL)
{
printf("Error: can't open file number one.\n");
}
if(file2 == NULL)
{
printf("Error: can't open file number two.\n");
}
else
{
/* if the files can open... start to check... */
}
}
You can compare the 2 files character by character (it doesn't matter if it's binary or text) and use a function that steps through the 2 files and compare character by character.
#include <stdio.h>
void compare(FILE *, FILE *);
int main(int argc, char *argv[]) {
FILE *file1, *file2;
file1 = fopen(argv[1], "r");
if (file1 == NULL) {
printf("Error: can't open file number one.\n");
return 0;
}
file2 = fopen(argv[2], "r");
if (file2 == NULL) {
printf("Error: can't open file number two.\n");
fclose(file1);
return 0;
}
if ((file1 != NULL) && (file2 != NULL)) {
compare(file1, file2);
}
}
/*
* compare two binary files
*/
void compare(FILE *file1, FILE *file2) {
char ch1, ch2;
int flag = 0;
while (((ch1 = fgetc(file1)) != EOF) && ((ch2 = fgetc(file2)) != EOF)) {
/*
* if equal then continue by comparing till the end of files
*/
if (ch1 == ch2) {
flag = 1;
continue;
}
/*
* If not equal then returns the byte position
*/
else {
fseek(file1, -1, SEEK_CUR);
flag = 0;
break;
}
}
if (flag == 0) {
printf("Two files are not equal : byte position at which two files differ is %d\n", ftell(file1) + 1);
}
else {
printf("Two files are Equal\n");
}
}
Test
$ ./a.out a.out a2.out
Two files are not equal : byte position at which two files differ is 209
$ cp a.out a2.out
$ ./a.out a.out a2.out
Two files are Equal
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How do get number line of file text on c ?. Help me .
Get sum number line.
I want read a file text.
EX:
for( line = 0; line < sumline; line ++) {
printf("char in line");
}
In case i understood the question :
#include <stdio.h>
#include <string.h>
main()
{
FILE *fp;
char * line;
size_t len = 0;
ssize_t read;
int lines = 0;
fp = fopen("input.txt", "r");
if( fp != NULL ){
while ((read = getline(&line, &len, fp)) != -1){
lines ++;
printf("%s\n", line);
}
fclose(fp);
}
printf("number of lines : %d\n", lines);
}
to count how many lines in your file
Try this:
`int lines = 0;
while ((read = getline(&line, &len, fp)) != -1) {
lines++;
}
cout << lines << endl;`
You can use the following function to get the number of lines inside a file.
#include <stdio.h>
// get the number of lines inside file
int getLineCnt(char *pcFileName) {
FILE *fp;
int lines=0;
fp = fopen(pcFileName, "r");
if(fp == NULL) { return -1; }
while (EOF != (fscanf(fp, "%*[^\n]"), fscanf(fp, "%*c"))) {
++lines;
}
io_fclose(fp);
return lines; ///\ retval number of lines
}