473,498 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C code and input files and output file

1 New Member
Hi Everyone,
I am trying to read two text files swY40p10t3ctw45.col.txt and solution.txt
and compare them, the first text file has a bunch of values listed like:
y[1,1,1,1]
y[1,1,1,2]
y[1,1,1,3]
y[1,1,1,4]
y[1,1,1,5]
y[1,1,1,6]
y[1,1,1,7]
y[1,1,1,8]
and so on..
and I am trying to compare it with a text file that looks like:
C0001 1.0000000
C0215 1.0000000
C0817 1.0000000
C0078 1.0000000

where the C0001 is the first variable down from the top of the list and C0215 is the 215 th variable down from the list. So I want to find from the first list the 215th and output y[#,#,#,#} and the solution value 1.00000 or whatever is right of the C0215.

The problem I am having is that I can build my current C code below, but I when I debug it I get "UNC paths are not supported. Defaulting to Windows directory, Cannot open file"

below is my code:
/*
** FILE SOURCE6.C
**
** ASSOCIATED FILES TO BE READ:
** File swY.col
** File solution.txt
************************************************** **********************
** The file is opened for reading. Each line is successively fetched
** using fgets command. The string is then divided up and put into a matrix.
** Matrix will be of 4 dimensional
************************************************** **********************
** Note that fgets returns NULL when there are no more lines in the file.
************************************************** **********************
** In this file swY.col consists of various decesion variables.
************************************************** **********************
** Typical data in swY.col might be: y[1,1,1,1]
************************************************** **********************
** y[1,1,1,1] = y[d,t,k,i]=y[day,test,type,started at]
************************************************** **********************
** SO WE READ THE LINE FROM THE TEXT FILE, THEN WE CONVERT THE INTERGER
** VALUES TO VALUES IN THE MATRIX SO IF decesion variable is y[1,2,4,5]
** THEN THAT LOCATION IS [1,2,4,5] in the matrix
**
** In this file solution consists of various solution values for a column.
************************************************** **********************
** Typical data in solution.txt might be: C0001 1.00000000000
************************************************** **********************
** It would be desirious to read in the say C001 and know it value in a matrix
************************************************** **********************
** LATER THE ASSOCIATED DECESION VARIABLE WILL BE LINKED TO THE SOLUTION VALUE
** AND THEN OUTPUT WILL BE ADJUSTED AND PUT TO A WORD FILE OR ANOTHER TXT FILE.




*/

#include <stdlib.h>
#include <stdio.h> /*standard*/
#include <math.h> /*for later functionality*/
#include <string.h> /*for reading strings*/
#include <fstream> /*to write to a file*/
#define FILENAME "finaloutput.txt"


FILE *fr; /* declare the file pointer */
FILE *f_r; /*second file pointer*/
FILE *r_f;
int d, t, k, i;

int main(void)
{
/*location in matrix of value*/


/*y[d,t,k,i, index value] */
/*int decesion_var[1][1][1][1][1]; */

/* this will before reading in the column variable name*/
/* will be put into a matrix*/
/*char column;*/

/*int variable_name[1][1][1]; /* [Column, COLUMN NUMBER, SOLUTION VALUE]*/

/*double solution_value;*/

int counter;

/*open a file to read results too*/
/*FILE *file = fopen("finaloutput.txt", "w");*/
r_f = fopen(FILENAME , "w");



/* to read charchters from the txt. file*/
struct air
{
/* in the form of y[#,#,#,#]*/

char a[1]; /*'y'*/

char b[1];/*'['*/

int c;/*'day'*/

char p[1];/*','*/

int e;/*'test'*/

char f[1];/*','*/

int g;/*'type'*/

char h[1];/*','*/

int w;/*'started time'*/
char j[1];/*']'*/
};
struct air ia;

/* to read charchters from output file*/
struct ground
{
/* in the form of C####*/

char column[1]; /*'C'*/

int variable; /*'####'*/

double solution_value; /*solution value*/
};
struct ground ib;



fr = fopen ("swY40p10t3ctw45.col.txt", "r"); /* open the file for reading */
f_r= fopen ("solution.txt", "r"); /*open soultuion file for reading*/
/* "rt" means open the file for reading text */


if(f_r==NULL)

{

puts("Cannot open file");
/* exit file*/
exit(0);

}
/*********************************/
/*READS IN COLUMN and associated VALUES*/
/*********************************/
else{
/* check to see that your not at the end of the file*/
char line[1000];

/*while(fgets (f_r,"%column %variable %solution_value\n", ib.column, &ib.variable, &ib.solution_value) != EOF)*/
while( fgets(line,1000,f_r) != NULL)
{
/* scan in the value for that line*/

/* sscanf (line, &variable_name[&column,&variable,&solution_value]);*/
scanf(line,"%column %variable %solution_value", &ib.column,&ib.variable,&ib.solution_value);

/* at every iteration counter will be reset to 1, so it can go through the list*/

counter = 1;

/*check to make sure you can open the decision variable file*/
if(fr==NULL)
{

printf("Cannot open file\n");
/* exit file*/
exit(0);

}


/*********************************/
/*READS IN THE DECESION VARIABLES*/
/*********************************/

/* check to see that your not at the end of the file and that you have no exceeded 1000*/
char line_2[1000];
/*while((fgets(fr,"%a %b %c %p %e %f %g %h %w %j\n", ia.a, ia.b, &ia.c, ia.p, &ia.e, ia.f, &ia.g, ia.h, &ia.w, ia.j) != EOF) && (counter<= 1000))*/
while(fgets(line_2,1000,fr) != NULL)
{
/* scan in the line from the text file to comapre*/

scanf(line_2, "%a %b %c %p %e %f %g %h %w %j", &ia.a, &ia.b, &ia.c, &ia.p, &ia.e, &ia.f, &ia.g, &ia.h, &ia.w, &ia.j);

if((counter != ib.variable) && (counter <= 1000))
{
/* add to the counter*/
counter = counter + 1;
}
/*compare it with the value scaned in from the optimal soulution*/

else if(counter == ib.variable)
{
/*if this is true then write this vallue to a txt. file*/
/* the output will be in the form of:
y[#,#,#] # (optimal solution value)
*/
fprintf(r_f, "%a %b %c %p %e %f %g %h %w %j %solution_value\n", ia.a, ia.b, ia.c, ia.p, ia.e, ia.f, ia.g, ia.h, ia.w, ia.j, ib.solution_value);
/*output this value to the screen*/
printf( "%a %b %c %p %e %f %g %h %w %j \t\t\t %solution_value\n", ia.a, ia.b, ia.c, ia.p, ia.e, ia.f, ia.g, ia.h, ia.w, ia.j, ib.solution_value);
/* set counter to 1001 to force the false statment and exit the while loop*/
counter = 1001;
}

}

}

fclose (fr); /* close the file */

}

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

fclose(r_f); /* close txt file*/


return(0);
} /*of main*/
Jan 4 '08 #1
1 2648
hdanw
61 New Member
Universal Naming Convention
Just a suggestion,

try to keep the post to the relavent information.

You have pages of useless information here and none of it helps resolve the issue.

It just puts a strain on those of us trying to help you.

Do you know how to trace the program? Can you isolate the few lines of code that generate the error?

Thats usually all we need.

I dont have the answer for you, and I bet a c-note that the person who does wont even have to look at your code to resolve your issue.

Happy coding.

Dan -
Feb 5 '08 #2

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

Similar topics

28
2739
by: wwj | last post by:
void main() { char* p="Hello"; printf("%s",p); *p='w'; printf("%s",p); }
14
9899
by: nullptr | last post by:
Hi, As an exercise, I've written a program implementing run-length encoding. I would be more that happy to hear your comments, suggestions, etc Thanks, --- #include <stdio.h> #include...
1
1828
by: Aalok | last post by:
This is what i want to do. Read a text file as an input and based on that file, Create an outpu text file which saves the contents of the input file in a specifi format in the output file. I...
8
1670
by: G Patel | last post by:
I wrote the following program to remove C89 type comments from stdin and send it to stdout (as per exercise in K&R2) and it works but I was hoping more experienced programmer would critique the...
135
7338
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about...
0
1660
by: CDMAPoster | last post by:
In: http://groups.google.com/group/comp.databases.ms-access/msg/9c3dcf952fc3e3d3 I said: '----- In: http://groups.google.com/group/comp.databases.ms-access/msg/c368352c1...
4
2733
by: georges the man | last post by:
hey guys, i ve been posting for the last week trying to understand some stuff about c and reading but unfortunaly i couldnt do this. i have to write the following code. this will be the last...
1
1523
by: gdarian216 | last post by:
okay I had a code that asked user to input a file name and instead i changed it to take the input from the command line. I now want to output results of some functions to an output file. I have...
61
3201
by: arnuld | last post by:
I have created a program which creates and renames files. I have described everything in comments. All I have is the cod-duplication. function like fopen, sprint and fwrite are being called again...
0
7167
Oralloy
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,...
1
6890
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...
0
7379
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5464
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,...
1
4915
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...
0
4593
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...
0
1423
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
657
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
292
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...

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.