I am new to programming and was able to compile the following source code in C++ using Visual Studio, but when I put the command that came with the source file foo < RTP300.cfg > text.cfg nothing happens. This command is supposed to generate a text file from a router configuration file. Where should the RTP300.cfg file that the command is pointing to be located? Any other info that can help would be greatly appreciated. Thanks!
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int decode(unsigned char);
int
decode(unsigned char ch)
{
ch = ~((ch << 2) | ((ch & 0xC0) >> 6));
if (ch) {
if (isprint(ch)) {
return ch;
} else {
return ' ';
}
}
return '\n';
}
int
main(int argc, char *argv[])
{
int ch;
FILE *fp;
if (argc < 2) {
fp = stdin;
} else {
if (NULL == (fp = fopen(argv[1], "r"))) {
fprintf(stderr, "Error: Cannot open file %s\n", argv[1]);
exit(EXIT_FAILURE);
}
}
while (EOF != (ch =fgetc(fp))) {
fputc(decode(ch), stdout);
}
return EXIT_SUCCESS;
}
Related
I have a file name stored as c-string. I need to open the file and count lines in it.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main() {
char str[] = "myfile.txt";
FILE* file = fopen(str, "r");
int counter = 0, ch = 0;
while (EOF != (ch = fgetc(file)))
if (ch == '\n')
++counter;
fclose(file);
printf("%d", counter);
return 0;
}
evrething works just fine if I use fopen("myfile.txt", "r") insted. How to make it work with a c-string file name.
please use string.c_str()
int main() {
string path = "myfile.txt";
FILE* file = fopen(path.c_str(), "r");
int counter = 0, ch = 0;
while (EOF != (ch = fgetc(file)))
if (ch == '\n')
++counter;
fclose(file);
printf("%d", counter);
return 0;
}
For my school project I must make a C++ program. The program has to use already installed applications on the Linux OS. To get familiar with managing other processes from within a C++ application I want to make a program that sets a wlan interface to monitor mode. The code that I have written so far is pretty long and doesn't seems efficient. Are there any ways to make my code more compact and efficient? At the end of the program I want to execute iwconfig to check if the wlan really is in monitor mode. What is the best way to do that?
#include <unistd.h>
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;
int main(int argc, char *argv[])
{
string wlan = "wlan1";
pid_t ifconfigDown;
ifconfigDown = fork();
if(ifconfigDown == 0)//ifconfig
{
execl("/sbin/ifconfig", "ifconfig", wlan.c_str(), "down",(char*)0 );
}
else //parent
{
usleep(500000);
pid_t iwconfigMode;
iwconfigMode = fork();
if(iwconfigMode == 0)//ifconfig
{
execl("/sbin/iwconfig","iwconfig",wlan.c_str(),"mode","monitor",(char*)0 );
}
else//parent
{
usleep(500000);
pid_t ifconfigUp;
ifconfigUp = fork();
if(ifconfigUp == 0)//ifconfig
{
execl("/sbin/ifconfig", "ifconfig", wlan.c_str(), "up", (char*)0 );
}
else//parent
{
usleep(500000);
pid_t iwconfig;
iwconfig = fork();
if(iwconfig == 0)//iwconfig
{
execl("/sbin/iwconfig", "iwconfig", (char*)0 );
//check if wlan1 is in monitor mode
}
}
}
waitpid(-1, NULL, 0);
return 0;
}
}
string rPopenEnd (string cmd)
{
FILE *fp = popen(cmd.c_str(), "r");
if (fp == NULL)
{
return "ERROR";
}
else
{
uint16_t line_size = 20;
char line[line_size];
string result;
while (fgets(line, line_size, fp))
result += line;
wait(NULL);
return result;
}
}
int main(int argc, char *argv[])
{
rPopenEnd("iwconfig");
}
I am trying to read two lines from a file using array of pointers. However, I am not getting anything on screen. I have tried searching online, but could not solve the problem. Here is my code that I have written using Netbeans on mac.
int main(int argc, char** argv) {
FILE *fp;
char *points[50];
char c;
int i=0;
fp=fopen("/Users/shubhamsharma/Desktop/data.txt","r");
if(fp==NULL)
{
printf("Reached here");
fprintf(stderr," Could not open the File!");
exit(1);
}
c=getc(fp);
while(c!=EOF)
{
*points[i]=c;
c=getc(fp);
i++;
}
for(int i=0;*points[i]!='\0';i++)
{
char d=*points[i];
printf("%c",d);
if(*(points[i+1])==',')
{
i=i+1;
}
}
return (EXIT_SUCCESS);
}
char *points[50];
Is not what you want, this is an array of 50 pointers to char.
If you want an array of pointers to char[50] you need:
char (*points)[50];
points = malloc(sizeof(*points) * 2);
Also note that fgets is prefered to get a line from a file
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
char (*points)[50];
points = malloc(sizeof(*points) * 2);
if (points == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
fp = fopen("/Users/shubhamsharma/Desktop/data.txt", "r");
if (fp == NULL) {
perror("fopen");
exit(EXIT_FAILURE);
}
fgets(points[0], sizeof(*points), fp);
fgets(points[1], sizeof(*points), fp);
fclose(fp);
printf("%s", points[0]);
printf("%s", points[1]);
free(points);
return 0;
}
Edit: I got the error. I need to put "books/" before the file name.
I'm reading the files names from a directory and then opening these files one by one.
I read the file and print its contents.
I have written a C++ code for this but its not working. I am not able to find any errors in it.
What am I doing wrong ?
My code -
#include <bits/stdc++.h>
#include <omp.h>
#include <dirent.h>
using namespace std;
int main(int argc, char* argv[]){
DIR *dirp;
struct dirent *dp;
if ((dirp = opendir("books")) == NULL) {
perror("couldn't open '.'");
return 0;
}
do {
errno = 0;
if ((dp = readdir(dirp)) != NULL) {
if(strlen(dp->d_name)>4){
string str;
int count=0;
ifstream in(dp->d_name);
//cout<<dp->d_name<<"....."<<endl;
while (!in.eof() && count<100) {
in >>str;
cout<<str;
count++;
}
in.close();
}
}
} while (dp != NULL);
if (errno != 0)
perror("error reading directory");
(void) closedir(dirp);
return 0;
}
Edit: I got the error. I need to put "books/" before the file name.
This code reads characters in a file and calculates length of characters. How i can read from second line and ignore read from first line?
this is part of my code:
int lenA = 0;
FILE * fileA;
char holder;
char *seqA=NULL;
char *temp=NULL;
fileA=fopen("d:\\str1.fa", "r");
if(fileA == NULL) {
perror ("Error opening 'str1.fa'\n");
exit(EXIT_FAILURE);
}
while((holder=fgetc(fileA)) != EOF) {
lenA++;
temp=(char*)realloc(seqA,lenA*sizeof(char));
if (temp!=NULL) {
seqA=temp;
seqA[lenA-1]=holder;
}
else {
free (seqA);
puts ("Error (re)allocating memory");
exit (1);
}
}
cout<<"Length seqA is: "<<lenA<<endl;
fclose(fileA);
Make a counter of how many \n you have seen,and when ==1 goto read from 2nd line.
int line=0;
while((holder=fgetc(fileA)) != EOF) {
if(holder == '\n') line++;
if(holder == 1) break; /* 1 because count start from 0,you know */
}
if(holder == EOF) {
//error:there's no a 2nd
}
while((holder=fgetc(fileA)) != EOF) {
// holder is contents begging from 2nd line
}
You can make it more simple by using fgets():
Make one call and ignore it(by don't discard the result-value,for error-checking);
Make second call, and begging reading from this.
NOTE: I'm considering C language here.
There is a tiny mistakes about the last answer.
I corrected and here is my code:
#include <stdio.h>
#include <stdlib.h>
#define TEMP_PATH "/FILEPATH/network_speed.txt"
int main( int argc, char *argv[] )
{
FILE *fp;
fp=fopen(TEMP_PATH, "r");
char holder;
int line=0;
while((holder=fgetc(fp)) != EOF) {
if(holder == '\n') line++;
if(line == 1) break; /* 1 because count start from 0,you know */
}
if(holder == EOF) {
printf("%s doesn't have the 2nd line\n", fp);
//error:there's no a 2nd
}
while((holder=fgetc(fp)) != EOF && (holder != '\n' )) {
putchar(holder);
}
fclose(fp);
}