472,992 Members | 3,294 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,992 software developers and data experts.

Reading from file and adding values to array

I am new to the C/C++

My Program:

int main(int argc, _TCHAR* argv[])

FILE *fp;
char line[1000];
char *CheckPoint[3000];

//Opening a file and reading

fp = fopen("C:\\config.txt","rt");
while(fgets(line, 1000, fp) != NULL)
char datatext[1000];
sscanf (line, "%s", &datatext);
printf ("%s\n", datatext);
CheckPoint[i] = datatext;

//Closing File
return 0;

//Config.txt Data


My Question:

I am able to read the data from the file from the above loop of reading the file
but when i add it to the array the value incrementing the array and assiging it, the values gets changed

first time from the txt file i get 10 and i add to CheckPoint[i] = datatext; where initially i =0, the next loop when the value for the datatext = 20 the initial value get to 20 and also inserts 20 in the second place

Where am i going wrong

Please help me
Nov 20 '07 #1
2 2383
112 100+

i think you didnt declare variable names i ?, and if you declare it and just you didnt post it within your code, you didnt increase it within loop

Best Regards
Nov 20 '07 #2
9,208 Expert Mod 8TB
There's a bigger bug here that just not declaring i.

There's this:
char datatext[1000];
sscanf (line, "%s", &datatext);
printf ("%s\n", datatext);
CheckPoint[i] = datatext;
Here CheckPoint[i] is assgined the address of the datatext array. As a result, every CheckPoint[i] will point to the same datatext array making the CheckPoint array elements to all be the same.

You need to a) find the length of the string in datatext, b) add 1 more for a null terminator, c) allocate memory for that length+1 and out that address in CheckPoint[i], d) copy datatext to the CheckPoint[i] array, e) write a loop at the nd of the program to delete all of the allocations made in the CheckPoint array.
Nov 20 '07 #3

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

Similar topics

by: SB | last post by:
Hello. I have an input file which is laid out in the following manner... Name Day 1 am time 1 am time 2 appointment pm time 1 pm time 2 appointment Day 2
by: Darsant | last post by:
I'm currently reading 1-n number of binary files, each with 3 different arrays of floats containing about 10,000 values a piece for a total of about 30,000 values per file. I'm looking for a way...
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
by: Anish G | last post by:
Hi, I have an issue with reading CSV files. I am to reading CSV file and putting it in a Datatable in C#. I am using a regular expression to read the values. Below is the code. Now, it reads...
by: dgleeson3 | last post by:
Hello All I have a txt file of strings of different lengths. I dont know how many strings are in the file. I have no problem reading the file and sending to the console (as below). To...
by: jordanbondo | last post by:
I have no clue why this isn't working. The top part that reads into size, shortTime, longTime, and threshold work fine. Down below where I try to do the exact same thing and read into x, i get...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.