Hi there,

I am trying to make a piece of code in C that:

1. opens a specified file,
2. Reads the data in the file and include it in MySQL, based on the position of commas,
3. Converts the last two number into binary and then performs some check on them
4. returns the new values to a second text file.

The data I have is in the format:

5128408,12.09,1,1,23604408,23604408
3515678,7.09,1,1,54321987,25467842
7432114,9.02,1,1,54204508,26754138
9412445,23.04,1,1,23777888,6543267

and I want to end up with database:
Info,amount.account,voucher,before,after


Any offers?

cheers

Recommended Answers

All 7 Replies

1. Read a line from the text file
2. Break the line apart using strtok() or some other method to put each field into its own variable.
3. Write the SQL INSERT or UPDATE statement
4. Execute the statement in #3 above
5. Make whatever checks you want on the data
6. Write out the new data to another text file.

MySQL has a C library/interface API that you will have to use, or you could use ODBC. Google around and you will find both.

Ancient Dragon pretty much answered the file and database side of the question.

Just wanted to add that if the numbers are strings, you need to convert them to integers with either the MySQL library functions (to read a field as an integer), or convert it with something like atoi(). Once its in integer form, its already in binary so you can do your work on that number.

If you need to access each bit from the integer in an easy way, use a mask to create an integer array like this:

unsigned int N = 23604408;  // the binary number

int BitArray[32];
int t = 0;
for (;t<32;t++)
  BitArray[t] = ((N & (1 << t)) > 0) ? 1 : 0;

Now you could access each bit individually by testing for 1 or 0 in BitArray[n].

If you need to be more memory efficient, use unsigned chars instead of int's, or just access the bits entirely using AND logic masking.

Hey can you help me in writing the SQL INSERT or UPDATE statement in C

Regards

hey i am using this code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>


FILE *fr;            /* declare the file pointer */

main()

{
   int n;
   char line[300];


   fr = fopen("C:\RECORD14.TXT", "r");  /* open the file for reading */

   while(fgets(line,300,fr) !=NULL )

   {
     /* get a line, up to 300 chars from fr.  done if NULL */

     printf ("%s\n",line);
   }
    ;

   fclose(fr);  /* close the file prior to exiting the routine */

}

and i am getting Debug Assertion Failed!!
expression (str!=NULL)

Can you help me solving this problem?

Thank you

I'm not completely sure why that specific error is happening there, but it might shed some light on things if you add this line of code to your program.

Add this immediately after you fopen command and see if it happens again.

if (fr == NULL) {
  fprintf(stderr, "Error opening file.\n");
  return -1;
}

My guess is that it will enter this code, at which point you need to review what your fopen is trying to accomplish.

Thanks for ur help..All went good.

While inserting data into table can i put variable instead of Values?

For examples:
if i have int number;
how can i insert the value of number into the table.Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.