::links not allowed, please read the Posting Guidelines::
The function readit() seems to jump to writeit() when it shouldn't in read.c This has got me stumped. After inside the readit() gets called it seems to when the function ends it seems to just process the writeit() when it isn't being called. Any help would be much appreciated :P
10 1752
I know the checkbuff() function is lame that's why it isn't being used.
I know the checkbuff() function is lame that's why it isn't being used.
Can we see a snippet of your code? The useful parts would probably be the declarations and the implementations of those functions, maybe a line or two before and after.
Well i will post 2 pages
It seems to be when the readit() gets called, it just jumps and writeit() gets processed
-------READ.C---------------- - #ifndef filesize
-
#define filesize 2001
-
#endif
-
-
struct tmpcommands{
-
FILE *fp;
-
};
-
-
extern void filehelp_text(char file[]){
-
printf("\nFILE FUNCTIONS: ");
-
printf("\nUsage is %s <COMMAND> <FILE> -f \n", file);
-
printf("Commands are -read, -append, -write, -delete\n");
-
printf("Remember the -f flag must be activated to use the file functions\n");
-
}
-
-
-
-
extern void readit(char file[]){
-
char filedata[80];
-
struct tmpcommands init;
-
init.fp = fopen(file, "r");
-
while(fgets(filedata, 80, init.fp)!=NULL)
-
printf("%s\n", filedata);
-
fclose(init.fp);
-
}
-
-
-
extern void writeit(char file[]){
-
char filedata[80];
-
struct tmpcommands init;
-
-
if(init.fp = fopen(file, "r"))
-
if(init.fp != NULL){
-
int hasbofprotect = 0;
-
char writedata[filesize];
-
char bofprotect[filesize];
-
fprintf(stdout, "The file contains data,\nplease enter some data to create the file and save the text\n");
-
fclose(init.fp);
-
init.fp = fopen(file, "w");
-
fscanf(stdin, "%s", &writedata);
-
-
if(strlen(writedata) >= filesize){
-
fprintf(stdout, "Error max file size length is 2000 bytes\n you entered %d bytes\n", strlen(writedata));
-
fprintf(stdout, "Please enter some text, remember max file size is 2000 bytes. :P\n");
-
fscanf(stdin, "%s", &bofprotect);
-
fprintf(init.fp, "%s", bofprotect);
-
hasbofprotect = 1;
-
}
-
-
if(hasbofprotect == 0){
-
fprintf(init.fp, "%s", writedata);
-
fprintf(stdout, "Everything is wrote to the file.\n");
-
fclose(init.fp);
-
}
-
-
}
-
else
-
{
-
char writedata[filesize];
-
-
-
-
fprintf(stdout, "You have chosen to write some data into the file: %s.\n", file);
-
-
-
}
-
}
-
-
extern void appit(char file[]){
-
char filedata[80];
-
struct tmpcommands init;
-
}
-
-
-
-
#ifdef filesize
-
#undef filesize
-
#endif
----MAIN.C----------- - #include <stdio.h>
-
#include <string.h>
-
#include <stdlib.h>
-
#include "main.h"
-
#include "safe.c"
-
#include "read.c"
-
-
/* checkbuff(buffer); A little command i made to check for bof in text which can be quite usefull */
-
-
static void command(char command[commandlen], char file[20], char flag[2]){
-
struct tmpcommands issue;
-
char com[10];
-
int iffile = 0;
-
-
if(strcmp(flag, "-f") != 1)
-
iffile = 1;
-
-
if(iffile != 0){
-
if(strcmp(command, "-read") != 1)
-
readit(file);
-
-
if(strcmp(command, "-write") != 1)
-
writeit(file);
-
-
if(strcmp(command, "-append") !=1)
-
appit(file);
-
-
}
-
}
-
-
-
int main(int argc, char *argv[]){
-
-
/* Main variables */
-
char buffer[max_buff];
-
char proccommand[commandlen];
-
int i=0;
-
-
if (argc < 4 ){
-
-
filehelp_text(argv[0]);
-
fprintf(stdout, "I am busy adding more commands, this program is still under development\n\n");
-
return 1;
-
-
}
-
-
command(argv[1], argv[2], argv[3]);
-
-
-
return 0;
-
}
if(strcmp(command, "-read") != 1)
readit(file);
if(strcmp(command, "-write") != 1)
writeit(file);
if(strcmp(command, "-append") !=1)
appit(file);
This is not good programming style. strcmp() returns a value of 0 when the comparison is equal and a value <0 when command is less than the second srgumennt. Both of these are !=1.
So if command is "-append", that's less than "-read", you you call readit().
Then "-append" is less than "-write" so you call writeit().
Then "-append" is equal to "-append" so you call appit().
Do you mean: -
if(strcmp(command, "-read") != 0)
-
readit(file);
-
-
if(strcmp(command, "-write") != 0)
-
writeit(file);
-
-
if(strcmp(command, "-append") !=0)
-
appit(file);
-
???
This is not good programming style. strcmp() returns a value of 0 when the comparison is equal and a value <0 when command is less than the second srgumennt. Both of these are !=1.
So if command is "-append", that's less than "-read", you you call readit().
Then "-append" is less than "-write" so you call writeit().
Then "-append" is equal to "-append" so you call appit().
Do you mean: -
if(strcmp(command, "-read") != 0)
-
readit(file);
-
-
if(strcmp(command, "-write") != 0)
-
writeit(file);
-
-
if(strcmp(command, "-append") !=0)
-
appit(file);
-
???
Well..... I thought strcmp returned 0 if the comparison is equal. I have changed it to !=0 and this is the result
doskey@DosKeys-Xion-sys:~/Desktop/project/src$ gcc main.c -o test
doskey@DosKeys-Xion-sys:~/Desktop/project/src$ ./test -read c -f
doskey@DosKeys-Xion-sys:~/Desktop/project/src$ ./test -read c -fa
The file contains data,
please enter some data to create the file and save the text
kjh
Everything is wrote to the file.
For some reason the -f flag needs another byte.... actually you can use any two bytes like -ba as an arguement. Also as you can see, when i used -read it still skipped and went to the writeit(). No matter what i put -append, -read, -write it's always jumping to the writeit() function.
This is the modified command() function -
static void command(char command[commandlen], char file[20], char flag[2]){
-
struct tmpcommands issue;
-
char com[10];
-
int iffile = 0;
-
-
if(strcmp(flag, "-f"))
-
iffile = 1;
-
-
if(iffile == 1){
-
if(strcmp(command, "-read")!=0)
-
readit(file);
-
-
if(strcmp(command, "-write")!=0)
-
writeit(file);
-
-
if(strcmp(command, "-append")!=0)
-
appit(file);
-
-
}
-
}
-
As you can see this program is doing stuff it hasn't been programmed to do.
I NEED HELP !!!!!!!!
How about you try: -
if(strcmp(command, "-read") == 0)
-
readit(file);
-
-
if(strcmp(command, "-write") == 0)
-
writeit(file);
-
-
if(strcmp(command, "-append") ==0)
-
appit(file);
-
My apology that I didn't see this on my previous post.
It seems it doesn't work. I don't know where i am going wrong here? HELP !!!!
Your problem is somewhere else. I took this code and made some stub functions and hard-coded a command. I then changed the command, recompiled. Each time I got the correct results: only one function was called. -
void readit()
-
{
-
cout << "readit" << endl;
-
}
-
void writeit()
-
{
-
cout << "writeit" << endl;
-
}
-
void appit()
-
{
-
cout << "appit" << endl;
-
}
-
int main()
-
{
-
char command[] = "-append";
-
if(strcmp(command, "-read") == 0)
-
readit();
-
-
if(strcmp(command, "-write") == 0)
-
writeit();
-
-
if(strcmp(command, "-append") ==0)
-
appit();
-
-
}
-
That's C++, you can't use C++ in your C code :S
That's C++, you can't use C++ in your C code :S
You are right. Old habits. Just change the cout to a printf.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Margaret MacDonald |
last post by:
I'm seeing a problem that has me flummoxed. The only thing I can
think of is that I'm violating some rule I don't know about.
I have some code that does some processing and then does a...
|
by: Sam Wuebben |
last post by:
I've been trying to get the values of hidden fields from one of many
forms on a page to load into a function.
The test page is at: http;//www.mvldesign.com/test/item_pop.html
The 'Add to Cart"...
|
by: JKop |
last post by:
Take the following simple function:
unsigned long Plus5Percent(unsigned long input)
{
return ( input + input / 20 );
}
Do yous ever consider the possibly more efficent:
|
by: brianj7675 |
last post by:
Basically i'm trying to write a previous next link in order to go to
the next page in my database(similar to google's with numbers in the
middle for pages the user wants to jump to.
My only...
|
by: anonymous |
last post by:
Thanks your reply.
The article I read is from
www.hakin9.org/en/attachments/stackoverflow_en.pdf.
And you're right. I don't know it very clearly. And that's why I want
to understand it; for it's...
|
by: baumann |
last post by:
hi all,
typedef int (*pfunc)(int , int);
pfunc a_func;
i know it's ok,
but how can define a_func without typedef statement?
thanks .
|
by: Todd A. Anderson |
last post by:
I have a function foo of which I need to get the address. The problem
is that when you say "&foo" (or just foo for that matter), you get the
address of this function's entry in a jump table and...
|
by: A Wieser |
last post by:
Hello,
It seems something's going on with regard to the way Visual Studio 2005
implements function pointers.
Once upon a time, it was possible to have unmanaged code as follows:
...
|
by: =?Utf-8?B?bGlhbnF0bGl0?= |
last post by:
Is using a jump statement more faster than using if statement
with a jump statement
example
for(int i=0; i < 10; i++)
{
if(a == null)
{
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |