LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-07-2011, 12:05 PM   #1
manu mittal
LQ Newbie
 
Registered: Aug 2011
Posts: 2

Rep: Reputation: Disabled
Unhappy c++ programming in unix


hey guys i am new to unix and programming in it plz help me.
i have written a code in unix

#include<iostream>
#include<unistd.h>
#include<fcntl.h>
using namespace std;
int main()
{
int fld;
char *buf;
fld=open("manu.txt",1);
cout<<"dsada";
int d=read(0,buf,1);
cout<<"manu";
cout<<d;
return 0;
}


the above code in output window cursor is blinking nothing else happens..
 
Old 08-07-2011, 12:36 PM   #2
flamelord
Member
 
Registered: Jun 2011
Distribution: Arch Linux
Posts: 151

Rep: Reputation: 34
The problem is that the output isn't being flushed. One way to flush is to send a new line (std::endl), or if you don't want a newline, send std::flush. Out of curiosity, why do you open "manu.txt" but never use it? And you might want to find a way to handle the rest of the input (including the newline from the user) or the rest of it will be sent to bash after the program terminates. Maybe read until the newline, and throw the information away.

Also, in order to read something, you need to assign some memory to your buffer, as it is now, you are trying to read a byte from the input into the position pointed to by a pointer that has not yet been assigned, which is a very bad idea. Since you are only using a single byte you should initialize it as
Code:
 char buf[1]
or make it bigger if you like.

Last edited by flamelord; 08-07-2011 at 12:46 PM.
 
1 members found this post helpful.
Old 08-07-2011, 04:23 PM   #3
baxzius
Member
 
Registered: Jan 2010
Location: India
Distribution: ubuntu
Posts: 134
Blog Entries: 1

Rep: Reputation: 21
Quote:
Originally Posted by flamelord View Post
The problem is that the output isn't being flushed. One way to flush is to send a new line (std::endl), or if you don't want a newline, send std::flush. Out of curiosity, why do you open "manu.txt" but never use it? And you might want to find a way to handle the rest of the input (including the newline from the user) or the rest of it will be sent to bash after the program terminates. Maybe read until the newline, and throw the information away.

Also, in order to read something, you need to assign some memory to your buffer, as it is now, you are trying to read a byte from the input into the position pointed to by a pointer that has not yet been assigned, which is a very bad idea. Since you are only using a single byte you should initialize it as
Code:
 char buf[1]
or make it bigger if you like.
thanks.
 
Old 08-07-2011, 10:09 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 08-08-2011, 02:12 AM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
For future reference, please post code between [code] tags to preserve indentation.

Also, you don't need to do IO like that. You can use the C++ facilities, e.g.

#include <fstream>
using namespace std;

(irrelevant code omitted)

ifstream in("somefile");
int x;

in >> x;

See, e.g. this.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c programming in unix dilberim82 Programming 7 04-21-2004 04:32 PM
UNIX (Linux, BSD, etc) Programming :: UNIX kuphryn Programming 8 04-04-2004 11:50 PM
is there actually a market for Unix programming? ShawnD Programming 37 05-16-2003 04:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration