1. The problem statement, all variables and given/known data:
Basically, I'm trying to search for a particular string pattern within a text file and print "textfile: line_no line". I need to read this in line at a time, but I'm restricted to using unbuffered I/O functions. I'm not sure if I'm missing something minor or if I'm misunderstanding how this works. The idea was to read() a small buffer (size 50), then to move the buffer over character at a time into a temp array until a newline is reached. Then use strstr to determine if the pattern occurs in the temp array, and if so, print the temp array to the screen.
I am reasonably sure that the problem lies within moving the buffer over to a temp array, and possibly with memset. Also, possible with read, because I keep getting a byte of trash every time I read into buf.
The output I'm getting for buf makes it appear that lseek is working perfectly fine, however, the temp array appears to be getting buf twice.
2. Relevant commands, code, scripts, algorithms:
Command used in bash shell: ./mycp <pattern> <filename>
3. The attempts at a solution (include all code and scripts):
I've attempted altering this line several times:
In place of "NULL", I've also used '/0' and '/n'. I've also tried strcpy() instead of copying over one character at a time. I get similar output, but amount of trash varies.
4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Arkansas State University, Jonesboro, AR, USA - Dr. Jiang CS4223
read() doesn't add NULLs to the end of your array, so checking for NULL won't work. read() DOES return how many bytes it read, so you could just stop there instead, but --
read() doesn't stop at the end of a line, either. You should read one by one and check for newlines yourself. You could try something like this:
You forgot to break on error or end-of-line, if you don't your for() loop won't actually stop.
Also this: buf[n+1] = '\0'; Remember, the code you copied that from added the \n to the data. That's why it needed +1, to not overwrite th \n. When not adding the \n, n+1 goes one too far, leaving a garbage byte in your string.
# Create the simulator object that we need in order to run NS
set ns
# Set the parameters that we will use for wireless communications
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagation model
set... (0 Replies)
Hi guys,
I recently discovered this problem and any help would be great.
1) Create a file in vi named Accounts_File with the following data:
The user, the name, the pin number, current balance and transaction history.
There are 3 users who need to be inputted with all their PIN, etc... (1 Reply)
Hello, everybody, I'm thankful for the great helps you gave during the past year.
Now I'm facing a serious problem:
I was obliged to write a 2D collision simulation applet, and my experience is only in C,C++,Intelx86 assembly. I have no experience in Java and the like, and I don't know... (2 Replies)
Hi,
I'm just start to learning simulate a network protocol using C/C++ and Java from scratch.
Actually, I want to make two nodes can communicate using TCP and UDP protocol, for example http connection and video streaming, respectively.
Can anyone help me find several references or guidance... (0 Replies)
Hey,
Task seems to be quite easy, but I'm still a bit green in shell scripting. I hope you can help me a bit. I have to run some simulation at the distance by remote terminal.
Normally when I'm working on the server directly I just type:
mpirun -np 8 compressibleInterFoam -parallel > log.txt... (7 Replies)
If i simulate a load on a solaris system to choke the system to 90% CPU usage .. what is likely to increase .. my systems fan rpms, cpu temperature, power drawn .. Can anybody tell me (4 Replies)
hello everybody.. im new to this forum.. i have sme basic knowledge about unix.. but not too much.. i would like to practice shell programs n perl using a unix simulator.. but then i don't know wht a unix simulator means? just a bald definition that it creates a unix working environment in windows... (5 Replies)
Ok, so I have a shell script which runs a specific command, and that command sends it's output to the display. At certain times, the command sends buffered output, and at other times, the command sends unbuffered output in the form of a % progress bar, so if I run the command, the output I receive... (0 Replies)