473,472 Members | 2,191 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

fopen manipulation (in C)

19 New Member
Please respond with answers in C.

Alright so lets say we have a structure:

Expand|Select|Wrap|Line Numbers
  1. typedef struct
  2. {
  3.     char Item[5];
  4.     char Descript[7];
  5. } INFO;
  6.  
Then lets say we fill it:

Expand|Select|Wrap|Line Numbers
  1. strcpy(INFO.Item,"A01");
  2. strcpy(INFO.Descript,"DA01");
  3.  
How would i then open a file using the string within INFO.Descript?

To show what i mean..(assume the file named DA01.txt exists)

Expand|Select|Wrap|Line Numbers
  1. fopen( "[string within INFO.Descript].txt" , "r"); //psuedo code
Jun 7 '08 #1
10 2390
oler1s
671 Recognized Expert Contributor
Your code won’t work. INFO is not a variable. It’s a typename. Look at what that typedef is doing. In any case, the concept you are looking for is string concatenation. There’s a function for that in C. It’s strcat. Or strncat.
Jun 8 '08 #2
odin607
19 New Member
the last line is psuedo code as the comment says, of course it doesnt work

I'll ask the same question in another way.

I need to open a file which is dependent on the value of INFO.Descript

if Descript is aa0001 i need it to open file aa0001.txt
if Descript is ab1234 i need it to open file ab1234.txt
etc..
Jun 8 '08 #3
oler1s
671 Recognized Expert Contributor
I’ll explain again.

the last line is psuedo code as the comment says, of course it doesnt work
Sure, but I wasn’t talking about the last line. When you do something like INFO.Descript, you treat INFO as a variable. It’s not a variable. INFO is the name of a new type you created (the struct). Do you see a problem with your code now?

if Descript is aa0001 i need it to open file aa0001.txt
if Descript is ab1234 i need it to open file ab1234.txt
etc..
Yes. What you are doing is concatenating “.txt” to some string. String concatenation.
Jun 8 '08 #4
odin607
19 New Member
ah, well assume i had made INFO INFO

so how would i then open the file once the file name in INFO.Descript
Jun 8 '08 #5
oler1s
671 Recognized Expert Contributor
so how would i then open the file once the file name in INFO.Descript
Concatenate the file extension onto the file name, then pass the augmented filename as a parameter to fopen. You already have the idea. Google C string concatenation, variations included, as well as look up strcat and strncat which I mentioned previously. There’s plenty of articles that show you code and theory for C string concatenation.

The basic principle is that you ensure your target string is big enough to hold the file extension. If it isn’t, you need to get a bigger set of space. Then use strcat or strncat to append to your target string.
Jun 8 '08 #6
Atos
7 New Member
Expand|Select|Wrap|Line Numbers
  1. <Solution Snipped>

I think that comments cover the most part...
Jun 15 '08 #7
Banfa
9,065 Recognized Expert Moderator Expert
Sorry Atos I have removed your solution to the problem. I would suggest you take a moment to read our Posting Guidelines. Particularly "How to respond to a question" which explains why we do not allow full code solutions on the site.

Banfa
Voice of Reason (and Administrator)
Jun 16 '08 #8
Atos
7 New Member
Sorry Atos I have removed your solution to the problem. I would suggest you take a moment to read our Posting Guidelines. Particularly "How to respond to a question" which explains why we do not allow full code solutions on the site.

Banfa
Voice of Reason (and Administrator)

if you had taken a moment to read my code you would have seen that it was a "explanatory" code and this was it's purpose. To show a way to solve a problem. Besides it was only a function.

If you think that by only reading books and writing code and NOT by READING CODE then...... ( i don't really have to say anything )
Jun 16 '08 #9
r035198x
13,262 MVP
if you had taken a moment to read my code you would have seen that it was a "explanatory" code and this was it's purpose. To show a way to solve a problem. Besides it was only a function.

If you think that by only reading books and writing code and NOT by READING CODE then...... ( i don't really have to say anything )
The problem is that you broke the site rules by posting a "full code solution". I agree that reading code helps a lot when learning but the site rules do not permit posting of full code solutions (also known as spoon-feeding).
Jun 16 '08 #10
odin607
19 New Member
yes and if you dont spoon feed a baby, itll die...

same deal with me, except not as morbid... i still dont know the solution because that was removed =p.. actually stopped making the program cause of this.. and got tired of having to ask 3000 questions/minute

but none the less if an answer could be sent to me (spoon fed or not) that would be awesome.. add it to the library of weird things I know how to do list.
Jul 21 '08 #11

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

Similar topics

3
by: Ming | last post by:
When I use fopen on the URL: fopen("http://home.pchome.com.tw/world/qoo4ko/110.jpg";, "r") I would get the following error message: -----------Error Message------------ Warning: fopen():...
7
by: git_cs | last post by:
Hey, guys and gals Somedays ago, I had asked for the DES algorithm in C language. Although I have written the algorthim in C myself, I am facing a peculiar problem, which I hope some of u guys and...
28
by: Sathyaish | last post by:
If fopen fails, is there a way to know why?
13
by: Blue | last post by:
Hi , Can any one please let me explain me the diffrences between "open"/ "fopen" or "read"/"fread" or "write/fwrite". I know that "open" /"read" / "write" are system calls and "fopen"...
10
by: pjlsr | last post by:
It's close to twenty years since I used the C language and at that time I was doing only floating point computational work, nothing with strings or reading files. I tried to use fopen in the...
10
by: Julia | last post by:
Hi, there, I am trying to append a binary file by using: FILE *strean; stream = fopen( "c:\temp.dat", "ba+" )); Is there a way that I can check if the file exists or not before fopen,...
25
by: subramanian100in | last post by:
Consider the following program: #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv) { if (argc != 2) { printf("Usage: <program-name<text-file>\n");
0
by: L'eau Prosper Research | last post by:
Press Release: L'eau Prosper Research (Website: http://www.leauprosper.com) releases new TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set. L'eau Prosper Market...
0
by: L'eau Prosper Research | last post by:
NEW TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set By L'eau Prosper Research Press Release: L'eau Prosper Research (Website: http://www.leauprosper.com) releases...
0
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,...
0
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
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
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
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
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.