I want to finish a program which can find a string in a file and then output a char 'T' in the end of line which the string exists . And my code like below(in this example I just find
a string "33"):
std::fstream f("D://test.txt");
char buff[256];
while(f.getline(buff,256)){
if(strstr(buff,"33")!= nullptr){
std::cout<<buff<<std::endl;
f.unget();//point to space
f.unget();//point to end of this line
f.seekp(f.tellg());
f<<'T';
break;
}
}
But it run wrong,as the pic show .What cause this fault?
And the origin test.txt just like:
value:32 char:
value:33 char:!
value:34 char:"
value:35 char:#
value:36 char:$
value:37 char:%
value:38 char:&
value:39 char:'
And the effect I want to get just like:
value:32 char:
value:33 char:T
value:34 char:"
...
Well! thanks the comment from "Igor Tandetnik". I try mycode in vs 2022, it runs well. Actually I run my code with mingw and clion in the beginning . I think this may be a bug of mingw.
I try to run a simple c++ code in a terminal and try to store its output in output.txt.
CODE-
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
while(1)
{
cin>>n;
cout<<n;
}
return 0;
}
using a terminal commands:
g++ code.cpp
./a.out > output.txt
and input:
1
2
3
4
5
but when I terminate the program by Ctrl+C, output.txt is empty.
Thanks for the help in advance.
for me even it is working as you expect. I think maybe your task is not closed! so try two things:
1- try to close the terminal after crtl+C. after again check the content of output file
2- please give a good name instead of a.out(testfile.out) and after crtl+c try below command in shell:
sudo ps -aux | grep testfile
to see that your process either was killed or not
I just check it out, I terminated the program by Ctrl+C, the output is perfectly storing into output.txt file.
But still, if you are not sure about the problem you can use the below method:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
while(n!=-1){
cin>>n;
if(n!=-1)
cout<<n<<endl;
}
return 0;
}
Input -1 to terminate the program. :)
I am trying to run the following simple code to check if scanf() is working.
#include<stdio.h>
int main(){
int num=0;
scanf("%d",&num);//Scanf not working
printf("The number is %d",num);
return 0;
}
However, it gives me an output of zero even though I enter some value for the variable 'num'.
For further clarification, I just installed turbo c++ in my new laptop. Is this causing some problem?
I've compiled a c++ file with Linux GCC compiler via eclipse , and it works fine without any error , but the problem that I face is that I cannot see any output from eclipse .
The code :
#include <iostream>
using namespace std;
int main() {
char reverseMe[] = {"reverseText"};
for(int v = sizeof(reverseMe) ; v >= 0 ; v--){
cout << reverseMe[v] << flush;
}
return 0;
}
In addition , I've launched the same code in online-c++-compiler , and it works fine and it gives me the result ! , but in eclipse , no output at all..
However , if I just replace 'flush' with 'endl' it gives me the result each character per one line (in eclipse),but I want the result to be in only one line.
So where is the problem ?
NOTE: I'm using Ubuntu 14 LTS , 64 bit
I have a simple C++ program that reads stdin using scanf and returns results to stdout using printf:
#include <iostream>
using namespace std;
int main()
{
int n, x;
int f=0, s=0, t=0;
scanf("%d",&n); scanf("%d",&x);
for(int index=0; index<n; index++)
{
scanf("%d",&f);
scanf("%d",&s);
scanf("%d",&t);
if(x < f)
{
printf("first\n");
}
else if(x<s)
{
printf("second\n");
}
else if(x<t)
{
printf("third\n");
}
else
{
printf("empty\n");
}
}
return 0;
}
I am compiling with g++ and running under linux. I execute the program using a text file as input, and pipe the output to another text file as follows:
program < in.txt > out.txt
The problem is that out.txt looks like this:
result1_
result2_
result3_
...
Where '_' is an extra space at the end of each line. I am viewing out.txt in gedit.
How can I produce output without the additional space?
My input file looks like this:
2 123
123 123 123
123 234 212
Edit: I was able to find a workaround for this issue: printf("\rfoo");
Thanks for your input!
Try removing the '\n' from your printf() statements, and run the code again. If the output file looks like one long word (no spaces), then you know that the only thing being inserted after the text is that '\n'.
I assume that the editor you are using to read the out.txt file just makes it look like there is an extra space after the output.
If you are still unsure, you can write a quick program to read in out.txt and determine the ASCII code of each character.
The end of line chars are:
System Hex Value Type
Mac 0D 13 CR
DOS 0D 0A 13 10 CR LF
Unix 0A 10 LF
For a end of line on each system you can:
printf("%c", 13);
printf("%c%c", 13, 10);
printf("%c", 10);
You can use this like
printf("empty");
printf("%c", 10);
Wikipedia Newline article here.
Okay, it's a little hard to figure this out, as the example program has numerous errors:
g++ -o example example.cc
example.cc: In function 'int main()':
example.cc:19: error: 'k' was not declared in this scope
example.cc:22: error: 'o' was not declared in this scope
example.cc:24: error: 'd' was not declared in this scope
make: *** [example] Error 1
But it's not going to be your input file; your scanf will be loading whatever you're typing into ints. This example, though:
/* scan -- try scanf */
#include <stdio.h>
int main(){
int n ;
(void) scanf("%d",&n);
printf("%d\n", n);
return 0;
}
produced this result:
bash $ ./scan | od -c
42
0000000 4 2 \n
0000003
on Mac OS/X. Get us a copy of the code you're actually running, and the results of od -c.
More information is needed here, as timhon asked, which environment are you working under? Linux, Windows, Mac? Also, what text editor are you using which displays these extra spaces?
My guess is that your space isn't really a space. Run
od -hc out.txt
to double check that it is really a space.
First, the code sample you've given doesn't compile as o and d are not defined...
Second, you've probably got whitespace at the end of the line you're reading in from the input file. Try opening it in vi to see. Otherwise, you can call a trim function on each line prior to output and be done with it.
Good luck!
Make sure you're looking at the output of the program you expect; this has a syntax error (no ";" after int n).
I feel like it's not even close to this, but if you run this on Windows, you'll get \r\n as line terminators, and, maybe, under *nix, under a non-Windows-aware text editor, you'll get \r as a common blank space, since \r is not printable.
Long shot, the best way to test this is using an hexadecimal editor and see the file yourself.