473,659 Members | 2,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

The function readit() seems to jump to writeit() when it shouldn't

10 New Member
::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
Sep 4 '07 #1
10 1770
doskey
10 New Member
I know the checkbuff() function is lame that's why it isn't being used.
Sep 4 '07 #2
sicarie
4,677 Recognized Expert Moderator Specialist
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.
Sep 4 '07 #3
doskey
10 New Member
Well i will post 2 pages
It seems to be when the readit() gets called, it just jumps and writeit() gets processed

-------READ.C----------------
Expand|Select|Wrap|Line Numbers
  1. #ifndef filesize
  2. #define filesize 2001
  3. #endif
  4.  
  5. struct tmpcommands{
  6.        FILE *fp;
  7.        };
  8.  
  9. extern void filehelp_text(char file[]){
  10.        printf("\nFILE FUNCTIONS: ");
  11.        printf("\nUsage is %s <COMMAND> <FILE> -f \n", file);
  12.        printf("Commands are -read, -append, -write, -delete\n");
  13.        printf("Remember the -f flag must be activated to use the file functions\n");
  14.        }
  15.  
  16.  
  17.  
  18. extern void readit(char file[]){
  19.        char filedata[80];
  20.        struct tmpcommands init;
  21.        init.fp = fopen(file, "r");
  22.        while(fgets(filedata, 80, init.fp)!=NULL)
  23.        printf("%s\n", filedata);
  24.        fclose(init.fp);
  25.        }
  26.  
  27.  
  28. extern void writeit(char file[]){
  29.       char filedata[80];
  30.       struct tmpcommands init;      
  31.  
  32.       if(init.fp = fopen(file, "r"))
  33.          if(init.fp != NULL){
  34.                int hasbofprotect = 0;
  35.                char writedata[filesize];
  36.                char bofprotect[filesize];
  37.                fprintf(stdout, "The file contains data,\nplease enter some data to create the file and save the text\n");
  38.                fclose(init.fp);
  39.                init.fp = fopen(file, "w");
  40.                fscanf(stdin, "%s", &writedata);
  41.  
  42.                if(strlen(writedata) >= filesize){
  43.                        fprintf(stdout, "Error max file size length is 2000 bytes\n you entered %d bytes\n", strlen(writedata));
  44.                        fprintf(stdout, "Please enter some text, remember max file size is 2000 bytes. :P\n");
  45.                        fscanf(stdin, "%s", &bofprotect);
  46.                        fprintf(init.fp, "%s", bofprotect);
  47.                        hasbofprotect = 1;
  48.                        }
  49.  
  50.                if(hasbofprotect == 0){
  51.                fprintf(init.fp, "%s", writedata);
  52.                fprintf(stdout, "Everything is wrote to the file.\n");
  53.                fclose(init.fp);
  54.                }
  55.  
  56.          }
  57.          else
  58.          {
  59.                char writedata[filesize];
  60.  
  61.  
  62.  
  63.       fprintf(stdout, "You have chosen to write some data into the file: %s.\n", file);
  64.  
  65.  
  66.       }
  67.       }
  68.  
  69. extern void appit(char file[]){
  70.       char filedata[80];
  71.       struct tmpcommands init;
  72.      }
  73.  
  74.  
  75.  
  76. #ifdef filesize
  77. #undef filesize
  78. #endif
----MAIN.C-----------

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "main.h"
  5. #include "safe.c"
  6. #include "read.c"
  7.  
  8. /* checkbuff(buffer); A little command i made to check for bof in text which can be quite usefull */
  9.  
  10. static void command(char command[commandlen], char file[20], char flag[2]){
  11.        struct tmpcommands issue;
  12.        char com[10];
  13.        int iffile = 0;
  14.  
  15.        if(strcmp(flag, "-f") != 1)
  16.          iffile = 1;
  17.  
  18.        if(iffile != 0){
  19.        if(strcmp(command, "-read") != 1)
  20.        readit(file);
  21.  
  22.        if(strcmp(command, "-write") != 1)
  23.        writeit(file);
  24.  
  25.        if(strcmp(command, "-append") !=1)
  26.        appit(file);
  27.  
  28.        }
  29.        }
  30.  
  31.  
  32. int main(int argc, char *argv[]){
  33.  
  34. /* Main variables */
  35. char buffer[max_buff];
  36. char proccommand[commandlen];
  37. int i=0;
  38.  
  39. if (argc < 4 ){
  40.  
  41. filehelp_text(argv[0]);
  42. fprintf(stdout, "I am busy adding more commands, this program is still under development\n\n"); 
  43. return 1;
  44.  
  45. }
  46.  
  47. command(argv[1], argv[2], argv[3]);
  48.  
  49.  
  50. return 0;
  51. }
Sep 5 '07 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
if(strcmp(comma nd, "-read") != 1)
readit(file);

if(strcmp(comma nd, "-write") != 1)
writeit(file);

if(strcmp(comma nd, "-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:

Expand|Select|Wrap|Line Numbers
  1. if(strcmp(command, "-read") != 0)
  2. readit(file);
  3.  
  4. if(strcmp(command, "-write") != 0)
  5. writeit(file);
  6.  
  7. if(strcmp(command, "-append") !=0)
  8. appit(file);
  9.  
???
Sep 5 '07 #5
doskey
10 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. if(strcmp(command, "-read") != 0)
  2. readit(file);
  3.  
  4. if(strcmp(command, "-write") != 0)
  5. writeit(file);
  6.  
  7. if(strcmp(command, "-append") !=0)
  8. appit(file);
  9.  
???

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

Expand|Select|Wrap|Line Numbers
  1. static void command(char command[commandlen], char file[20], char flag[2]){
  2.        struct tmpcommands issue;
  3.        char com[10];
  4.        int iffile = 0;
  5.  
  6.        if(strcmp(flag, "-f"))
  7.          iffile = 1;
  8.  
  9.        if(iffile == 1){
  10.        if(strcmp(command, "-read")!=0)
  11.        readit(file);
  12.  
  13.        if(strcmp(command, "-write")!=0)
  14.        writeit(file);
  15.  
  16.        if(strcmp(command, "-append")!=0)
  17.        appit(file);
  18.  
  19.        }
  20.        }
  21.  
As you can see this program is doing stuff it hasn't been programmed to do.
I NEED HELP !!!!!!!!
Sep 6 '07 #6
weaknessforcats
9,208 Recognized Expert Moderator Expert
How about you try:

Expand|Select|Wrap|Line Numbers
  1. if(strcmp(command, "-read") == 0)
  2. readit(file);
  3.  
  4. if(strcmp(command, "-write") == 0)
  5. writeit(file);
  6.  
  7. if(strcmp(command, "-append") ==0)
  8. appit(file);
  9.  
My apology that I didn't see this on my previous post.
Sep 8 '07 #7
doskey
10 New Member
It seems it doesn't work. I don't know where i am going wrong here? HELP !!!!
Oct 5 '07 #8
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Expand|Select|Wrap|Line Numbers
  1. void readit()
  2. {
  3.    cout << "readit" << endl;
  4. }
  5. void writeit()
  6. {
  7.    cout << "writeit" << endl;
  8. }
  9. void appit()
  10. {
  11.    cout << "appit" << endl;
  12. }
  13. int main()
  14. {
  15.       char command[] = "-append";
  16.       if(strcmp(command, "-read") == 0)
  17.         readit();
  18.  
  19.         if(strcmp(command, "-write") == 0)
  20.         writeit();
  21.  
  22.         if(strcmp(command, "-append") ==0)
  23.         appit();
  24.  
  25. }
  26.  
Oct 5 '07 #9
doskey
10 New Member
That's C++, you can't use C++ in your C code :S
Oct 11 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

10
5639
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 header('Location: ...) jump to page A on success or falls through to the jump to page B. This is the code: if ( mysql_query( 'LOCK TABLES tableX WRITE', $link ) ) { mysql_query( $q, $link ) ; // store the record
1
1549
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" image needs to send that forms data to the function seen in the head. Firebird's javascript Console states that "form has no properties"
11
2700
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:
1
1758
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 thought is i'm losing the parameters on page reload. But if that is the case then I don't know the correct technique to keep the variables I am programming in php but using a javscript function with onclick. I could use $_SESSION variables to retain...
4
3616
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 useful to help me to solve some basic problem which I may not perceive before. I appreciate your help, sincerely.
42
5601
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 .
6
5034
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 not the address of the function itself. Are there any BKMs for getting the real address or am I going to have to write a function that looks to see whether the address is a jump instruction and if so compute the real address from the jump target?...
2
1117
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: __declspec(naked) void HookJump(...) { _asm
1
1795
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) {
0
8427
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8332
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8525
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7356
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6179
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.