summary : system("clear"); isn't working well.
I'm using gcc, ubuntu 18.04 LTS version for c programming.
what I intended was "read each words and print from two text files. After finish read file, delay 3 seconds and erase terminal"
so I was make two text files, and using system("clear"); to erase terminal.
here is whole code.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
void printFiles(char *file1,char *file2,char *change1, char *change2){
FILE *f;
char *text = malloc(sizeof(char)*100);
f=fopen(file1,"r");
system("clear");
//while(!feof(f)){
while(EOF!=fscanf(f,"%s",text)){
//fscanf(f,"%s", text);
printf("%s ",text);
//usleep(20000);
}
//sleep(3);
fclose(f);
printf("\n");
//all comment problems are appear here. and if I give delay, such as usleep() or sleep, delay also appear here. Not appear each wrote part.
f=fopen(file2,"r");
//while(!feof(f)){
while(EOF!=fscanf(f,"%s",text)){
if(strcmp(text,"**,")==0){
strcpy(text,change1);
strcat(text,",");
}
else if(strcmp(text,"**")==0){
strcpy(text,change1);
}
else if(strcmp(text,"##.")==0){
strcpy(text,change2);
strcat(text,".");
}
else if(strcmp(text,"##,")==0){
strcpy(text,change2);
strcat(text,",");
}
printf("%s ",text);
//usleep(200000);
}
fclose(f);
free(text);
sleep(3); //here is problem. This part works in the above commented part "//all comment problems are appear here."
system("clear"); //here is problem. This part works in the above commented part "//all comment problems are appear here."
}
int main(){
char file1[100] = "./file1.txt";
char file2[100] = "./file2.txt";
char change1[100]="text1";
char change2[100]="text2";
printFiles(file1,file2,change1,change2);
return 0;
}
I'm very sorry, files and variables names are changed because of policy. Also, file contents also can not upload.
I can't find which part makes break Procedure-oriented programming. I think that was compiler error, because using one file read and system(clear); works well.
I also make two point variables, such as 'FILE *f1; FILE *f2; f1=fopen(file1); f2=fopen(file2)...`, but same result occur.
Is it compiler error? If it is, what should I do for fix these problem? Thanks.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
void printFiles(char *file1,char *file2,char *change1, char *change2){
FILE *f;
char *text = malloc(sizeof(char)*100);
f=fopen(file1,"r");
system("clear");
//while(!feof(f)){
while(EOF!=fscanf(f,"%s",text)){
//fscanf(f,"%s", text);
printf("%s ",text);
fflush(stdout);
//usleep(20000);
}
//sleep(3);
fclose(f);
printf("\n");
//all comment problems are appear here. and if I give delay, such as usleep() or sleep, delay also appear here. Not appear each wrote part.
f=fopen(file2,"r");
//while(!feof(f)){
while(EOF!=fscanf(f,"%s",text)){
if(strcmp(text,"**,")==0){
strcpy(text,change1);
strcat(text,",");
}
else if(strcmp(text,"**")==0){
strcpy(text,change1);
}
else if(strcmp(text,"##.")==0){
strcpy(text,change2);
strcat(text,".");
}
else if(strcmp(text,"##,")==0){
strcpy(text,change2);
strcat(text,",");
}
printf("%s ",text);
fflush(stdout);// The answer.
//usleep(200000);
}
fclose(f);
free(text);
sleep(3); //here is problem. This part works in the above commented part "//all comment problems are appear here."
system("clear"); //here is problem. This part works in the above commented part "//all comment problems are appear here."
}
int main(){
char file1[100] = "./file1.txt";
char file2[100] = "./file2.txt";
char change1[100]="text1";
char change2[100]="text2";
printFiles(file1,file2,change1,change2);
return 0;
}
Hint for
That's probably just buffering. Do fflush(stdout); before you sleep. – melpomene
Thanks.
You can try this solution for delay.
#include <time.h>
#include <stdio.h>
void delay(double seconds)
{
const time_t start = time(NULL);
time_t current;
do
{
time(¤t);
} while(difftime(current, start) < seconds);
}
int main(void)
{
printf("Just waiting...\n");
delay(3);
printf("...oh man, waiting for so long...\n");
return 0;
}
Following solution is pretty quite the same of previous one but with a clear terminal solution.
#include <time.h>
#include <stdio.h>
#ifdef _WIN32
#define CLEAR_SCREEN system ("cls");
#else
#define CLEAR_SCREEN puts("\x1b[H\x1b[2J");
#endif
void delay(double seconds)
{
const time_t start = time(NULL);
time_t current;
do
{
time(¤t);
} while(difftime(current, start) < seconds);
}
int main(void)
{
printf("Just waiting...\n");
delay(2); //seconds
printf("...oh man, waiting for so long...\n");
delay(1);
CLEAR_SCREEN
return 0;
}
I am making a program that downloads files to a windows. To do so i used urlmon and the urldownload to file function. Whenever i download a file with the function in question in my windows i only get a prefetch file, but i can't find the file on my hard drive. Please tell me what i am doing wrong?
#include <windows.h>
#include <stdio.h>
typedef HRESULT (WINAPI *UDTF)(LPVOID, LPCTSTR, LPCTSTR, DWORD, LPVOID);
int dl_url(char *url, char *path)
{
int q = 1;
HMODULE hDll;
UDTF URLDownloadToFile;
if((hDll = LoadLibrary("urlmon")))
{
if((URLDownloadToFile = (UDTF)GetProcAddress(hDll, "URLDownloadToFileA")))
{
if(URLDownloadToFile(0, url, path, 0, 0) == 0)
q = 0;
}
FreeLibrary(hDll);
}
return q;
}
Note: I use a 32 bit windows xp to test this program.
I have written a small program which insert the value and its corresponding value into the Windows registry key.
Program is working fine but it is not inserting value and its corresponding value.
And one more thing when I run prog as an administrator RegSetValueEx() fails..but still inssert only the value not its data.
Please help for finding out the issue here.
My code is as follows..
#define WIN32_LEAN_AND_MEAN
#define WIN32_DEFAULT_LIBS
#ifndef _WIN32_WINNT
#define _WIN32_WINNT (0x0601)
#endif /* _WIN32_WINNT */
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <tchar.h>
#include <unistd.h>
#include <stdbool.h>
#include<string.h>
BOOL InstallRunOnStartup()
{
HKEY key;
long result;
BOOL ret = FALSE;
LPTSTR val=L"12as3d12";
LPTSTR a=L"zzz";
TCHAR szBuf[20];
result = RegOpenKeyEx(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", NULL, KEY_WRITE | KEY_WOW64_64KEY | KEY_SET_VALUE , &key);
if (result == ERROR_SUCCESS)
{
printf("hi \n");
if (RegSetValueEx(key, a, 0, REG_SZ,(LPBYTE)val, (DWORD)(lstrlen(val)+1) == ERROR_SUCCESS)){
printf("success \n");
ret = TRUE;
}
RegCloseKey(key);
}
return ret;
}
int main()
{
InstallRunOnStartup();
getch();
}
Add your program to the following path in Windows XP:
C:\Documents and Settings\All Users\Start Menu\Programs\Startup
Did you try assigning LPBYTE(val) to a temp variable & using that?
RegSetValueEx expects the buffer in bytes & the number of bytes in that buffer.
If LPTSTR is defined as Unicode in your project, then lstrlen(val) will return the length of the string which is half the size of your byte array.
I tried a different program and it worked..
HKEY hMykey; //Handle to your key
DWORD pDWDisp; // Ignore for this
LONG lRes; // Test Success
char prog[] = "\"C:\\a.exe\""; //Key to launch
lRes = RegCreateKeyEx(HKEY_LOCAL_MACHINE,
"Software\\Microsoft\\Windows\\CurrentVersion\\run",
0,"Whatever",REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS ,
NULL,&hMykey,&pDWDisp); // Open a key for edit
if(lRes != ERROR_SUCCESS){
MessageBox(0,"Error opening key","",MB_OK);
return false;
//exit(0);// Shutdown on fail
}
lRes = RegSetValueEx(hMykey,"a",0,REG_SZ,
(LPBYTE)prog,strlen(prog)+1);// Add your key value
if(lRes != ERROR_SUCCESS){
MessageBox(0,"Error saving record","",MB_OK);
RegCloseKey(hMykey);
return false;
//exit(0);// Shutdown on fail
}
MessageBox(0,"Success!! Registry value recorded","",MB_OK);
RegCloseKey(hMykey);
return true;
I am getting an error that makes no sense. I have the following code -
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <grp.h>
gid_t groupIdFromName(const char *name)
{
struct group *grp;
gid_t g;
char *endptr;
if (name == NULL || *name == '\0')
return -1;
g = strtol(name, &endptr, 10);
if (*endptr == '\0')
return g;
grp = getgrnam(name);
if (grp == NULL)
return -1;
return grp->gr_gid;
}
int main(int argc, char** argv) {
return (EXIT_SUCCESS);
}
I am using NetBeans in Linux Mint and when I try and while my program builds without problems, when I try and run it I get the following error -
Signal received: SIGSEGV (?) with sigcode ? (?)
From process: ?
If I comment out the line grp = getgrnam(name); this error goes away. But I don't understand why this line should cause it to fail, particularly since my main method is blank.
I have found that many people have been experiencing this bug for quite some time in NetBeans and Code::Blocks. I tried the code in Eclipse and it works fine. So don't use NetBeans for C development on Linux is my advice.
Following is the final code I was working on. I can sleep and show again other messages with sleep() but I can't print what I originally wanted which is inside 2nd while loop. As far as I tested, while((ptr=getutent()) != NULL) would be the problem but I don't know how to solve. I would really appreciate if anyone can help me. Thanks
Note: The program is showing current login user for every 5 seconds until user stop with Ctrl+c.
#include <utmp.h>
#include <pwd.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(void)
{
struct utmp *ptr;
struct passwd *pwd;
while(1)
{
while((ptr=getutent()) != NULL)
{
if(ptr->ut_type==USER_PROCESS)
{
pwd=getpwnam(ptr->ut_user);
printf("USERNAME = %s | ID = %d | GID = %d | ",p
tr->ut_user,pwd->pw_uid,pwd->pw_gid);
printf("HomeDir = %s | HOST = %s\n",pwd->pw_dir,
ptr->ut_host);
}
}
sleep(3);
fflush(stdout);
}
}
You want to use setutent() to set the file pointer back to the beginning of the utmp file.